Docunext


ActiveModel and ActiveRelation

September 17th, 2011

Active Model is a new component of Rails 3+ that abstracts out some generic functionality from ActiveRecord to that a single API can be included with different ORMs and data storage mechanisms.

In my experience, this mostly helps with validations, and that is quite useful, but it also adds many handle methods like new_record? and destroyed?, to name a couple.

What about persisted? No, that's an ActiveRecord method. There are other ORMs that support persisted, though, too, such as the Git interface included in Regulate.

Speaking of which, the Git interface included in regulate is a very nice abstraction, but I'd prefer if it were more abstract. Its too tied into what regulate is all about, yet it sort of tries to be a little bit like ActiveRecord. I wonder if it could be replaced by gitmodel...?

Active Model with Associations?

I would really love it if Active Model could do associations, and had assumed for awhile that it would. But then I thought about my recent experiments with Arel and how it is able to create SQL joins based upon associations. I'd prefer that to having Ruby build all the associations manually.

But still, its a really cool idea: to be able to create associations across different object models? Like setting static data as an array of hashes in the class definition.

But wait! I imagine that it would be possible to have non-AR association method makers - something like "model_has_many", which would generate methods to define object-based relationships rather than SQL-based relationships. As a very simple example, it is possible to create AR*-like* methods manually:

class Person < ActiveRecord::Base

  def accounts
    Account.all.select{|a| a.person_id = self.id}
  end
end

Of course, the Person class would need to have an all method tha returned an array, in the above example.

I have done some research on this topic because I felt certain that other developers must have been interested in this very cool idea. I found a project that seem to abstract out something like this:

and I found a related, albeit difficult to read, PDF:

Other related projects to Active Model and Active Relation:

¥

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022