I have a special access method in my rails 3.1.6 application that assigns a value to an attribute, even if that value is missing. The my_attr attribute is a serialized hash that should be combined with this value if only an empty value is specified, in which case it sets the current value to an empty value. (Added checks to make sure the values are what they should be, but removed for brevity, as they are not part of my question.) My setter is defined as:
def my_attr=(new_val)
cur_val = read_attribute(:my_attr)
write_attribute(:my_attr, {}) if (new_val.nil? || new_val.blank? || cur_val.blank?)
if cur_val.blank?
write_attribute(:my_attr, new_val)
else
write_attribute(:my_attr,cur_val.deep_merge(new_val))
end
read_attribute(:my_attr)
end
This code works well as it is, but not when I use self.write_attribute (). Then I get the following error:
NoMethodError:
private method `write_attribute' called for
: write_attribute, ? - Ruby Rails ( )?