Jump to content

The ultimate community for Ruby on Rails developers.


Photo

What is ActiveRecords::Base?

ActiveRecords

  • Please log in to reply
1 reply to this topic

#1 Varun_krishna

Varun_krishna

    Signalman

  • Members
  • 23 posts

Posted 02 June 2014 - 04:06 AM

HI all

Whenever I generate a model in Rails 3 I am seeing the ActiveRecords::Base is getting added to the class names automatically and I would like to know the explanation of this ActiveRecords::Base,  is ActiveRecords is a class or a module, then what is Base object for the ActiveRecords ?

 

 

 

 

Thanks

Varun Krishna. P



#2 Ohm

Ohm

    Driver

  • Moderators
  • 450 posts
  • LocationCopenhagen

Posted 02 June 2014 - 05:44 AM

Yes, ActiveRecord::Base is a class, the ActiveRecord part being the module.

 

It's all to do with inheritance. If you inherit from another class, you get all it's methods and accessors. Here's an example:

class Person
  attr_accessor :name, :gender

  def initialize(name, gender)
    @name = name
    @gender = gender
  end
end

class Male < Person
  def initialize(name)
    super(name, :male)
  end
end

class Female < Person
  def initialize(name)
    super(name, :female)
  end
end 

f = Female.new('Ann') 
puts f.name # => Ann 
puts f.gender # => female

Both Male and Female knows about name and gender, however we only specified it in the Person class.

 

This is the same way we do with ActiveRecord::Base. When we inherit from it, we get all the methods needed to become a database table with records and such.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk






Also tagged with one or more of these keywords: ActiveRecords

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users