Difference between Active Model, Active Record and Active Resource

Rails 3 is designed with modularity in mind. Each module has its own purpose and functionality.

ActiveModel: This component was created in Rails 3. They took all the model related parts that did not have a database requirement of Rails 2 ActiveRecord and moved it into ActiveModel. So ActiveModel includes things like validations. More information: http://www.rubyinside.com/rails-3-0s-activemodel-how-to-give-ruby-classes-some-activerecord-magic-2937.html

ActiveRecord: This is the component that associates a class to the database. This will give the class functionality such as methods that make it easy to pull records from the database (An example is the find method).

ActiveResource: Similar to ActiveRecord. However, instead of being backed by a database, an ActiveResource object is backed by another application through a web service API. More information: http://ofps.oreilly.com/titles/9780596521424/activeresource_id59243.html

(Couldn't figure out about ActiveBase... where did you hear it from?)


What I understand:

ActiveModel + Database Support = ActiveRecord

ActiveModel via WebService API = ActiveResource


ActiveModel https://github.com/rails/rails/tree/master/activemodel

Think of a super model who is in constant need of validation.

ActiveModel can be used for many things, but mostly recognized for adding validation support to models / db records.


ActiveRecord https://github.com/rails/rails/tree/master/activerecord

Think record as in table record.

Sets up a mapping between a new class and an existing table in a database.

In the context of an app, these classes are commonly referred to as models. Models can also be connected to other models; this is done by defining associations.

  class Firm < ActiveRecord::Base
    has_many   :clients
    has_one    :account
    belongs_to :conglomerate
  end

In the background, rails uses ActiveRecord for schema management and defining properties for your records, acting as an ORM (object relational mapper):

"ORM: An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data."

A schema outlines the properties for a record.


ActiveResource https://github.com/rails/activeresource

Think resource like the R in URL or of the resource routing that powers many rails backends.

Allows you to do things like Create, Retrieve, Update, or Destroy (CRUD) via HTTP.

  tyler = Person.find(1) 

When a request is made to a resource route, a RESTful request maps itself its corresponding HTTP verbs and their database interactions

  GET    => Person.find(1)
  POST   => Person.new(:name => 'Tyler', :favorite_page => 'stackoverflow') 
  PUT    => tyler.save
  DELETE => tyler.destroy