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 10 years, and have been enjoying Ruby on Rails for the past 5.
My experience covers communities, shopping solutions, multi-language sites, heavy back-end lifting and a wide selection of more traditional websites. I like to integrate Ruby with Java and .NET through JRuby and IronRuby when it makes sense. I am passionate about test- and behavior-driven development, but at the same time I am pragmatic and believe in getting things done.
I live in Copenhagen, Denmark, where I work for a fantastic company: Podio. I do not currently take on freelance assignments.