I have 3 fields in my form witch are not in my database: opening_type, opening_hours, opening_minutes. I want to update the main attribute "opening" (in database) with these 3 fields.
I tried lot of things that doesn't work.
Actually I have:
attr_accessor :opening_type, :opening_hours, :opening_minutes
def opening_type=(opening_type)
end
def opening_type
opening_type = opening.split("-")[0] if !opening.blank?
end
def opening_hours=(opening_hours)
end
def opening_hours
opening_hours = opening.split("-")[1] if !opening.blank?
end
def opening_minutes=(opening_minutes)
end
def opening_minutes
opening_minutes = opening.split("-")[2] if !opening.blank?
end
I tried adding something like:
def opening=(opening)
logger.info "WRITE"
if !opening_type.blank? and !opening_hours.blank? and opening_minutes.blank?
opening = ""
opening << opening_type if !opening_type.blank?
opening << "-"
opening << opening_hours if !opening_hours.blank?
opening << "-"
opening << opening_minutes if !opening_minutes.blank?
end
write_attribute(:opening, opening)
end
def opening
read_attribute(:opening)
end
But, the accessors methods are not called and I think opening_type, opening_hours, opening_minutes were empty too if the accessors were called...
I think I don't need a before_save callback and should do this rewriting the accessors.
Notes: - Rails 3.0.5, - opening_type, :opening_hours, :opening_minutes could be empty
EDIT: I updated my code