# Using ActiveRecord Find with associations

**URL:** <https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408>\
**Category:** Rails\
**Created:** [September 4, 2010, 2:27am UTC](https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408 "2010-09-04T02:27:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [September 4, 2010, 2:27am UTC](https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408/1 "2010-09-04T02:27:31Z")

</div>

Here’s an example:

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

class Order \< ActiveRecord::Base  
has\_many :line\_items  
end

my\_line\_item = LineItem.find(:zipcode =\> “12345”)

Order.find\_by\_line\_item(my\_line\_item) # doesn’t work

Order.find\_by\_line\_item\_id (my\_line\_item) # does work

I was surprised by this, because it seems to force me to make reference  
to the id field of the line item, rather than referring to the LineItem  
as a class or type in the Order.findxxx statement.

Am I misunderstanding something or missing a trick?

Thanks!!

– Pito

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [September 4, 2010, 5:18am UTC](https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408/2 "2010-09-04T05:18:48Z")

</div>

Pito S. wrote:

> Here’s an example:
> 
> class LineItem \< ActiveRecord::Base  
> belongs\_to :order  
> end
> 
> class Order \< ActiveRecord::Base  
> has\_many :line\_items  
> end
> 
> my\_line\_item = LineItem.find(:zipcode =\> “12345”)
> 
> Order.find\_by\_line\_item(my\_line\_item) # doesn’t work
> 
> Order.find\_by\_line\_item\_id (my\_line\_item) # does work

Probably not explaining this clearly enough (I blame the distillery  
masters at Lagavulin), but:

find\_by\_line\_item fails - my\_line\_item is an instance of the class (an  
object) that does not have a ‘line\_item’ attribute you indicated, it  
_is_ a LineItem

find\_by\_line\_item\_id works - rails infers the id attribute when you pass  
an object (my\_line\_item), and in this case you’ve provided enough  
information to tell rails which attribute to use…

I suppose you could mod the logic for find to include your syntax…

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [September 4, 2010, 1:24pm UTC](https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408/3 "2010-09-04T13:24:22Z")

</div>

Ar Chron wrote:

> Pito S. wrote:
> 
> > Here’s an example:
> > 
> > class LineItem \< ActiveRecord::Base  
> > belongs\_to :order  
> > end
> > 
> > class Order \< ActiveRecord::Base  
> > has\_many :line\_items  
> > end
> > 
> > my\_line\_item = LineItem.find(:zipcode =\> “12345”)
> > 
> > Order.find\_by\_line\_item(my\_line\_item) # doesn’t work
> > 
> > Order.find\_by\_line\_item\_id (my\_line\_item) # does work
> 
> Probably not explaining this clearly enough (I blame the distillery  
> masters at Lagavulin), but:

Wish I was there! Which of their Beer is available in the US?

> find\_by\_line\_item fails - my\_line\_item is an instance of the class (an  
> object) that does not have a ‘line\_item’ attribute you indicated, it  
> _is_ a LineItem

It kind of does, in that Order has\_many :line\_items, no? And so that’s  
an ActiveRecord method on Order called line\_items?

By the way, my\_order.find\_by\_line\_item fails with “no such method”, not  
just by returning [].

I wonder if my\_order.find\_by\_line\_items would work?

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408/4 "2010-09-06T16:39:35Z")

</div>

As I am sure you know the ‘find\_by\*’ methods are all dynamic. They are  
based on the field/column name, not on the type of object the column  
is supposed to reference (if any). AR will do the magic of finding the  
ID value if you pass an object to the method but apparently it will  
not be “magic enough” to let you remove the ‘\_id’ suffix from the  
field/column name in your ‘find\_by\*’ method name. It would be nice if  
the Rails guys made a little change so we could do what you wanted to  
do here, though. 😉

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408/5 "2010-09-06T16:39:37Z")

</div>

> > Probably not explaining this clearly enough (I blame the  
> > distillery
> 
> > masters at Lagavulin), but:
> 
> Wish I was there! Which of their Beer is available in the US?

Lol, Lagavulin is a famous WHISKY destillery

> By the way, my\_order.find\_by\_line\_item fails with “no such method”, not  
> just by returning [].

ActiveRecord knows that your line\_item belongs to an order.  
So it’s as easy as:  
my\_line\_item.Order

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/using-activerecord-find-with-associations/194408/6 "2010-09-06T16:39:39Z")

</div>

On 4 September 2010 01:27, Pito S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s an example:
> 
> class LineItem \< ActiveRecord::Base  
> belongs\_to :order  
> end
> 
> class Order \< ActiveRecord::Base  
> has\_many :line\_items  
> end
> 
> my\_line\_item = LineItem.find(:zipcode =\> “12345”)
> 
> Order.find\_by\_line\_item(my\_line\_item) # doesn’t work
> 
> Order.find\_by\_line\_item\_id (my\_line\_item) # does work
> 
> I was surprised by this, because it seems to force me to make reference  
> to the id field of the line item, rather than referring to the LineItem  
> as a class or type in the Order.findxxx statement.
> 
> Am I misunderstanding something or missing a trick?

I think you are missing a trick. Once you have my\_line\_item, to get  
the item’s order just do  
my\_line\_item.order  
There is no need to use find at all.

Colin
