Public Service Announcement: Rails 2.1 Dirty Objects.

0 views
Skip to first unread message

Tony Hillerson

unread,
Jul 3, 2008, 4:59:00 PM7/3/08
to rubyamf
In the back of my mind I wondered if this was going to be a problem,
and sure enough, it is.

One of the new features in Rails 2.1 is called "partial updates". You
can change a model:

Person.create :name => "Joe"
=> true
person = Person.find_by_name "Joe"
=> ...
person.name = "Bob Dobbs"
=> "Bob Dobbs"

and then query if the model changed:

person.changed?
=> true
person.changes
=> {"name" => ["Joe", "Bob Dobbs"]}
person.save
=> true

And the SQL will be:
UPDATE `people` SET `updated_at` = '2008-07-03 20:44:19', `name` =
'Bob Dobbs' WHERE `id` = 1

The upshot is that Rails can do less bulk updates, less communication
with the database, and use less resources overall.

RubyAMF doesn't work that way when saving. When RubyAMF sends a new
object in it automatically creates an ActiveRecord, not checking with
the database at all so Rails doesn't think it changed:

person.changed?
=> false

That means it won't save, since Rails thinks it hasn't changed. Big
problems.

The workaround for now is if you're using RubyAMF with Rails 2.1 you
need to turn off partial_updates, probably in environment.rb

# on one model:
Person.partial_updates = false

# on all models:
ActiveRecord::Base.partial_updates = false

The long term fix for this is to be more Rails-like and communicate
using Hashes instead of automatically constructing ActiveRecords,
which we're already thinking about.

Sorry for any inconvenience, and let me know if anyone else runs into
this.
Reply all
Reply to author
Forward
0 new messages