# Help with ActiveRecord for Models with long associtation chain

**URL:** <https://www.ruby-forum.com/t/help-with-activerecord-for-models-with-long-associtation-chain/139078>\
**Category:** Rails\
**Created:** [May 30, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/help-with-activerecord-for-models-with-long-associtation-chain/139078 "2008-05-30T16:52:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [May 30, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/help-with-activerecord-for-models-with-long-associtation-chain/139078/1 "2008-05-30T16:52:14Z")

</div>

Hi,

I am trying to use this set of associations for what must be a fairly  
standard data model, to give me the accessor @batch.products

Batch:  
has\_many :orders  
has\_many :line\_items :through=\>orders  
Order:  
has\_many :line\_items  
belongs\_to :batch  
has\_many :products, :through=\>:line\_items  
LineItem:  
belongs\_to :product  
belongs\_to :order  
Product::  
has\_many :line\_items

OK I think they are the main associations involved.

I sort of want to say that Batch  
has\_many :products, :through=\>orders, :through=\>line\_items

This doesnt work and I have tried other variations. The only way i  
can think at the moment is to make products into a named scope using  
an sql find, but I am sure there must be a way of telling AR how to do  
the joins without using sql.

TRANSACTION question?  
I want to get to @batch.products so that I can iterate through each  
product and update the product stock quantities based on the line\_item  
quantities. Then mark the batch as updated and finally commit as a  
single batch save. My thinking being that this would be carried out  
as a safe transaction. The alternative, would be to build a specific  
transaction, but I am not sure how to do this across 2 models (ie  
Batch and Product)

All help much appreciated.  
Tonypm

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [May 30, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/help-with-activerecord-for-models-with-long-associtation-chain/139078/2 "2008-05-30T17:45:44Z")

</div>

On May 30, 2:15 pm, tonypm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the joins without using sql.

has\_many :through can’t be nested (I’ve vague feeling there’s a plugin  
that does this).  
You could probably simplify the find down by using :joins (ie  
something like  
Product.find :all, :joins =\> {:line\_items =\> {:order =\> :batch}},  
:conditions =\> {‘batch.id’ =\> some\_batch\_id

> TRANSACTION question?  
> I want to get to @batch.products so that I can iterate through each  
> product and update the product stock quantities based on the line\_item  
> quantities. Then mark the batch as updated and finally commit as a  
> single batch save. My thinking being that this would be carried out  
> as a safe transaction. The alternative, would be to build a specific  
> transaction, but I am not sure how to do this across 2 models (ie  
> Batch and Product)

Unless batch and product are in different databases, Batch.transaction  
and Product.transaction are exactly the same. If you don’t like  
‘preferring’ one over the other you could always do  
ActiveRecord::Base.transaction do  
…  
end

Fred

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [May 30, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/help-with-activerecord-for-models-with-long-associtation-chain/139078/3 "2008-05-30T18:05:18Z")

</div>

Fred,

thanks for very quick response. That clarifies things and helps me  
move forward.

Actually my update is complicated further, since a line\_item can be a  
packet with a bill of materials of several products so transaction  
will be the way to go. and I see I dont need to worry about tables  
since the transaction is on the actual connection not the database.

Many Thanks  
Tonypm

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [May 30, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/help-with-activerecord-for-models-with-long-associtation-chain/139078/4 "2008-05-30T18:12:23Z")

</div>

Fred - Oh and by the way!

Product.find :all, :joins=\>{:line\_items=\>{:order  
=\> :batch}}, :conditions=\>{‘batches.id’=\>361}

worked perfectly - wouldnt have figured that out myself!

Thanks  
Tonypm
