ActiveRecord is one of the central components of Rails… unfortunately, quite bad one.
Perhaps I should explain one detail first – it is a perfectly good ORM if you are writing something as simple as a blog. That’s what Rails can be used for successfully. But if you want to write a more complex application, this becomes a nightmare.
The first problem I want to describe is inherent problem of the active record as a design pattern – there is no identity map, so same object might be loaded into the application multiple times. This does not only have negactive impact on performance, but also leads to some hard-to-find bugs.
Lets consider a simple has_many/belongs_to relation (one-to-many). Sometimes one might want to iterate through all the objects in the collection and call some method of them, which in turns calls some method of the parent object. But here is the problem – each child object will load new copy of the parent, so each method call would go to different copy. A simple solution is to add the parent object as a parameter to the method of child objects, but in more complex situations it becomes a nightmare to trace.
Here goes the code sample:
class ParentObject < ActiveRecord::Base
has_many :child_objectsdef do_sth_on_children
child_objects.each { |c| c.upd_parent}
end
def do_sth
end
end
class ChildObject < ActiveRecord::Base
belongs_to :parent_object
def upd_parent
parent_object.do_sth
end
end
I know there is a plugin for ActiveRecord that addresses this problem, at least in the “data consistency” layer, but leaves the performance problem untouched
September 9, 2009 at 11:43 pm |
datamapper I guess
September 10, 2009 at 12:06 am |
@roger – Sure. But if you throw away ActionView (which is very limited when compared to eg. Mako or Genshi) and ActiveRecord then what remains? ActionController + some glue code? Not worth going into rails for complex sites, in my opinion.