You can create virtual attributes in your model to represent these structures.
There is railscast in this question, but in the end you can do something similar in your model
def full_name [first_name, last_name].join(' ') end def full_name=(name) split = name.split(' ', 2) self.first_name = split.first self.last_name = split.last end
If you want to explicitly change the attribute value when reading or writing, you can use the read_attribute or write_attribute methods. (Although I believe that they may be outdated). A.
These works replace the access method of this attribute with your own. For example, a branch identifier field may be entered as xxxxxx or xx-xx-xx. This way you can change your branch_identifier = method to remove hyphens when data is stored in the database. This can be done like this:
def branch_identifier=(value) write_attribute(:branch_identifier, value.gsub(/-/, '')) unless value.blank? end
Steve weet
source share