One of the new features in Rails 2.1 is dirty models, which in turn allows for partial updates.
However, this can result in fields not being updated in the database, if you do not use the conventional setter attributes. One unconventional way of updating an attribute, is to change the object of a serialized attribute - typically a Hash.
If I have this model:
class Page < ActiveRecord::Base serialize :properties, Hash end
- then this code will not update my serialized field in the database:
p = Page.first p.properties[:color] = 'fff' p.save
- since I haven’t assigned anything to properties, but just changed the contents of the Hash inside properties.
To get the properties field properly updated in the database, I need to call properties_will_change! like this:
p = Page.first p.properties_will_change! p.properties[:color] = 'fff' p.save
I wonder if this is a bug or a feature … ?
Hello, I'm Casper Fabricius. I have developed for the web for 8 years, and have been enjoying Ruby on Rails for the past 3.
My experience covers communities, shopping solutions, multi-language sites, heavy back-end lifting and a wide selection of more traditional websites. I currently favor Radiant CMS as a platform, and I am an expert Radiant extension developer.
I am based in Copenhagen, Denmark, but I take assignments from across the globe. Feel free to study my resumé, featured projects and - of course - to hire me.