Is find() a class method or instance method?

Dear friends,

I came across this code snippet from my reading:

require 'active_record'

class Order < ActiveRecord::Base
end

order = Order.find(1)
...

Looking at: http://api.rubyonrails.org/ and search for find(), find() is
an instance public method. So why can we call Order.find(1) here?
Shouldn’t it be Order.new.find(1)?

On Monday, January 6, 2014 7:53:22 AM UTC, Ruby-Forum.com User wrote:

order = Order.find(1)
...

Looking at: http://api.rubyonrails.org/ and search for find(), find() is
an instance public method. So why can we call Order.find(1) here?
Shouldn’t it be Order.new.find(1)?

No, because although it is a public instance method, it’s a public
instance
method of ActiveRecord::Relation, not of ActiveRecord::Base.
ActiveRecord
delegates the find method (and the other querying type methods such as
where, first, all etc.) to an instance of this

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs