Difference between Active Model, Active Record and Active Resource
Asked Answered
U

3

73

Is there anyone who can help me by defining the exact difference between Active Model, Active Record and Active Resource. I have done enough googling in order to find the exact difference, but didn't get anything concrete which can tell the exact difference between them. Right now they all look same to me. So please give me the appropriate answer with some concrete points.

Unbroken answered 29/9, 2012 at 13:56 Comment(0)
H
108

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?)

Hildahildagard answered 7/10, 2012 at 1:54 Comment(6)
Thanks richardaday for such clear cut answer. Sorry for the wrong word "ActiveBase"..its "ActiveRecord::Base". Can you just tell me what this is? I know that Active record inherits from it but what is the actual use of "ActiveRecord::Base". Please tell me also also send me the link for detail answer if possible.Unbroken
ActiveRecord::Base is ActiveRecord. I think you may be confused with the syntax ActiveRecord::Base: ActiveRecord is the module grouping together multiple classes that implement it's functionality (an example class would be Base). ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html and api.rubyonrails.org/classes/ActiveRecord/Base.htmlHildahildagard
The O'Reilly link no longer links to what it originally did.Selsyn
Here's the archived O'Reilly link.Athalla
Please add ActiveRelationship in this answer too.Retaretable
Note that ActiveRecord::Base (as of Rails 6.0.3.2 at any rate) includes all the ActiveModel submodules that ActiveModel::Model includes -- AttributeAssignment, Conversion, ForbiddenAttributesProtection, Model, Validations, Validations::HelperMethods -- and more besides: AttributeMethods, Dirty, SecurePassword, Serialization, Serializers::JSON, Validations::Callbacks.Quadratic
E
20

What I understand:

ActiveModel + Database Support = ActiveRecord

ActiveModel via WebService API = ActiveResource

Enervate answered 13/12, 2013 at 11:2 Comment(0)
M
9

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
Mutate answered 10/10, 2014 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.