# Association chaining

**URL:** <https://www.ruby-forum.com/t/association-chaining/125605>\
**Category:** Rails\
**Created:** [January 10, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/association-chaining/125605 "2008-01-10T15:21:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![renek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3be4f8/32.png) [@renek](https://www.ruby-forum.com/u/renek)\
**Post date:** [January 10, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/association-chaining/125605/1 "2008-01-10T15:21:07Z")

</div>

Hello,

I am trying to setup some basic association chaining. I have the  
following models:

class User \< ActiveRecord::Base  
has\_many :posts  
end

class Post \< ActiveRecord::Base  
has\_many :tags  
belongs\_to :user  
end

class Tag \< ActiveRecord::Base  
belongs\_to :post  
end

I made some basic migrations and created the database tables.  
Then I start script/console and start creating some data:

Loading development environment (Rails 2.0.2)

> > User.create  
> > =\> #\<User id: 1, created\_at: “2008-01-10 15:16:06”, updated\_at:  
> > “2008-01-10 15:16:06”\>
> 
> > Post.create(:user\_id =\> 1)  
> > =\> #\<Post id: 1, user\_id: 1, created\_at: “2008-01-10 15:16:16”,  
> > updated\_at: “2008-01-10 15:16:16”\>
> 
> > Tag.create(:post\_id =\> 1)  
> > =\> #\<Tag id: 1, post\_id: 1, created\_at: “2008-01-10 15:16:27”,  
> > updated\_at: “2008-01-10 15:16:27”\>

So far so good. But now I want to test the associations.

First find a tag:

> > Tag.find(:first)  
> > =\> #\<Tag id: 1, post\_id: 1, created\_at: “2008-01-10 15:16:27”,  
> > updated\_at: “2008-01-10 15:16:27”\>

Then find the associated post:

> > Tag.find(:first).post  
> > =\> #\<Post id: 1, user\_id: 1, created\_at: “2008-01-10 15:16:16”,  
> > updated\_at: “2008-01-10 15:16:16”\>

Still fine, but it breaks if I want to chain the association further:

> > Tag.find(:first).post.user  
> > ActiveRecord::RecordNotFound: Couldn’t find User without an ID  
> > from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/  
> > active\_record/base.rb:1248:in `find_from_ids' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ active_record/base.rb:504:in`find’  
> > from /usr/lib/ruby/gems/1.8/gems/utility\_belt-1.0.6/lib/  
> > rails\_finder\_shortcut.rb:12:in `send' from /usr/lib/ruby/gems/1.8/gems/utility_belt-1.0.6/lib/ rails_finder_shortcut.rb:12:in`user’  
> > from (irb):6

I tried different models, rails versions, but can not find what I am  
doing wrong… Anyone?

Ren

---

<div class="post-metadata">

**Author:** ![renek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3be4f8/32.png) [@renek](https://www.ruby-forum.com/u/renek)\
**Post date:** [January 10, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/association-chaining/125605/2 "2008-01-10T18:19:15Z")

</div>

I found my problem. It is within the utilitybelt gem I use for my irb  
sessions. This apparently does not allow association chaining.

On 10 jan, 15:20, renek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :tags  
> Loading development environment (Rails 2.0.2)\>\> User.create  
> So far so good. But now I want to test the associations.
> 
> rails\_finder\_shortcut.rb:12:in `user’  
> from (irb):6
> 
> I tried different models, rails versions, but can not find what I am  
> doing wrong… Anyone?
> 
> René
