Getting related object from the active record

Hello.

How do I access blog object/model from the following active record
class:

class Entry < ActiveRecord::Base

belongs_to :blog

def self.test
puts blog.title
end
end

In the example above I want to print the blog title.

Thanks for help!

On Oct 23, 3:56 pm, Aljaz F. [email protected]
wrote:

puts blog.title

end
end

Don’t make test a class method - individual instances of Entry have
an associated blog, not the class as a whole.

Fred

Quoting Aljaz F. [email protected]:

def self.test
puts blog.title
end
end

In the example above I want to print the blog title.

The class doesn’t have a blog, individual entries have blogs.

entry = Entry.find(…whatever, :include => :blog)
puts entry.blog.title

The :include option “eager loads” the blog at the same time as the
entry.
Without it, the blog will be loaded when the puts statement executes.

HTH,
Jeffrey

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