# Getting related object from the active record

**URL:** <https://www.ruby-forum.com/t/getting-related-object-from-the-active-record/177779>\
**Category:** Rails\
**Created:** [October 23, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/getting-related-object-from-the-active-record/177779 "2009-10-23T16:56:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [October 23, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/getting-related-object-from-the-active-record/177779/1 "2009-10-23T16:56:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [October 23, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/getting-related-object-from-the-active-record/177779/2 "2009-10-23T17:31:29Z")

</div>

On Oct 23, 3:56 pm, Aljaz F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![introvert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c37758/32.png) [@introvert](https://www.ruby-forum.com/u/introvert)\
**Post date:** [October 24, 2009, 12:42am UTC](https://www.ruby-forum.com/t/getting-related-object-from-the-active-record/177779/3 "2009-10-24T00:42:58Z")

</div>

Quoting Aljaz F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
