# Associations Problem

**URL:** <https://www.ruby-forum.com/t/associations-problem/191861>\
**Category:** Rails\
**Created:** [July 19, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/associations-problem/191861 "2010-07-19T22:28:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leo72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/71c47a/32.png) [@leo72](https://www.ruby-forum.com/u/leo72)\
**Post date:** [July 19, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/associations-problem/191861/1 "2010-07-19T22:28:13Z")

</div>

Hi guys,  
I’m having a strange error in one of my model relationships and it’s  
driving me crazy

I have:  
class Target \< ActiveRecord::Base  
has\_many :sections  
end

class Section \< ActiveRecord::Base  
belongs\_to :target  
has\_many :line\_items  
end

class LineItem \< ActiveRecord::Base  
belongs\_to :section  
end

When I do the following in script/console:  
s = LineItem.last.section (s.id is 21, as is LineItem.last.section\_id as  
well)  
s.target

I get the Section instance, and the logger shows no query to the  
targets table. However, if I do:  
s = Section.find(21)  
s.target

I get the right Target instance.

Even more strange is that if I do:  
s = LineItem.last.section  
s.target.target (twice the target message)

I get the right Target instance.

I’m using Rails 2.3.5 and I’m getting this warning, but I’m not sure  
if it has something to do with it:  
vendor/rails/activerecord/lib/active\_record/associations/association\_proxy.rb:217:  
warning: default `to\_a’ will be obsolete

Any clue?

Thanx a lot in advance.

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![leo72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/71c47a/32.png) [@leo72](https://www.ruby-forum.com/u/leo72)\
**Post date:** [July 19, 2010, 11:11pm UTC](https://www.ruby-forum.com/t/associations-problem/191861/2 "2010-07-19T23:11:33Z")

</div>

On Jul 19, 9:26 pm, Leonardo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Even more strange is that if I do:  
> s = LineItem.last.section  
> s.target.target (twice the target message)
> 
> I get the right Target instance.

If you do some\_line\_item.section what you get back isn’t a section but  
an association proxy. If you do anything that actually requires the  
section object, active record will load it and send the method calls  
to it. Unfortunately for you the method on association proxy that  
returns the actual object for the association is called target.

Fred

---

<div class="post-metadata">

**Author:** ![leo72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/71c47a/32.png) [@leo72](https://www.ruby-forum.com/u/leo72)\
**Post date:** [July 20, 2010, 9:48am UTC](https://www.ruby-forum.com/t/associations-problem/191861/3 "2010-07-20T09:48:23Z")

</div>

On 19 July 2010 22:09, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > s = Section.find(21)
> 
> If you do some\_line\_item.section what you get back isn’t a section but  
> an association proxy. If you do anything that actually requires the  
> section object, active record will load it and send the method calls  
> to it. Unfortunately for you the method on association proxy that  
> returns the actual object for the association is called target.

See [http://wiki.rubyonrails.org/rails/pages/ReservedWords](http://wiki.rubyonrails.org/rails/pages/ReservedWords) for more  
words you can’t use.

Colin

---

<div class="post-metadata">

**Author:** ![leo72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/71c47a/32.png) [@leo72](https://www.ruby-forum.com/u/leo72)\
**Post date:** [July 20, 2010, 2:45pm UTC](https://www.ruby-forum.com/t/associations-problem/191861/4 "2010-07-20T14:45:16Z")

</div>

On Tue, Jul 20, 2010 at 4:46 AM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > targets table. However, if I do:
> > 
> > If you do some\_line\_item.section what you get back isn’t a section but  
> > an association proxy. If you do anything that actually requires the  
> > section object, active record will load it and send the method calls  
> > to it. Unfortunately for you the method on association proxy that  
> > returns the actual object for the association is called target.  
> > Thanks a lot, Frederick. Just the words I needed.

> See [http://wiki.rubyonrails.org/rails/pages/ReservedWords](http://wiki.rubyonrails.org/rails/pages/ReservedWords) for more  
> words you can’t use.  
> Thanks Colin!, great tip! I didn’t know this.

–  
Leonardo M…  
There’s no place like ~
