# Multiple associated records

**URL:** <https://www.ruby-forum.com/t/multiple-associated-records/171628>\
**Category:** Rails\
**Created:** [July 6, 2009, 8:17pm UTC](https://www.ruby-forum.com/t/multiple-associated-records/171628 "2009-07-06T20:17:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 6, 2009, 8:17pm UTC](https://www.ruby-forum.com/t/multiple-associated-records/171628/1 "2009-07-06T20:17:45Z")

</div>

I am dealing with a legacy db which is limiting my options here.

I have…

class Stockmaster \< ActiveRecord::Base  
set\_table\_name “stockmaster”  
set\_primary\_key “stockid”

has\_many :prices

def wholesale\_price(stockid)  
Price.find(:first, :conditions =\> [“stockid = ? AND typeabbrev = ?”,  
stockid, “WH”])  
end

def retail\_price(stockid)  
Price.find(:first, :conditions =\> [“stockid = ? AND typeabbrev = ?”,  
stockid, “RE”])  
end  
end

and

class Price \< ActiveRecord::Base  
set\_table\_name “prices”  
set\_primary\_key “stockid”

belongs\_to :stockmaster  
end

and my issue is that I have both a ‘WH’ and ‘RE’ typeabbrev for each  
stockid and I need to make this simpler for a database lookup to print a  
prices sheet.

It makes sense to do eager loading of associations here but I cannot  
figure out how to change the model for this eager loading since for each  
stockid, there are two values (WH/wholesale and RE/retail).

Can anyone toss me a bone here?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 6, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/multiple-associated-records/171628/2 "2009-07-06T22:24:01Z")

</div>

On Mon, 2009-07-06 at 11:16 -0700, Craig W. wrote:

> def wholesale\_price(stockid)  
> and  
> prices sheet.
> 
> It makes sense to do eager loading of associations here but I cannot  
> figure out how to change the model for this eager loading since for each  
> stockid, there are two values (WH/wholesale and RE/retail).
> 
> Can anyone toss me a bone here?

* * *

still trying but can’t seem to find a way for this to work (in  
Stockmaster class),

has\_one :wh\_price, :through =\> ‘prices’, :conditions =\> [“typeabbrev  
= ?”, “WH”], :source =\> ‘prices’  
has\_one :re\_price, :through =\> ‘prices’, :conditions =\> [“typeabbrev  
= ?”, “RE”], :source =\> ‘prices’

but when I restart console and check,

> > @stkitems[0].wh\_price  
> > ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the  
> > association “prices” in model Stockmaster

from  
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/reflection.rb:285:in  
`check_validity!' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/has_many_through_association.rb:5:in`initialize’ from  
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/associations.rb:1230:in  
`new' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1230:in`wh\_price’  
from (irb):6  
from :0

> >

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 7, 2009, 7:07am UTC](https://www.ruby-forum.com/t/multiple-associated-records/171628/3 "2009-07-07T07:07:36Z")

</div>

Shouldn’t the line in Prices be  
belongs\_to :stockmaster, :foreign\_key =\> ‘stockid’  
since ActiveRecord will be looking for ‘stockmaster\_id’?

I’m not sure I’m on the right track there, but in your first example  
you were explicitly looking for stockid in the find.  
Cheers.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 7, 2009, 1:37pm UTC](https://www.ruby-forum.com/t/multiple-associated-records/171628/4 "2009-07-07T13:37:09Z")

</div>

On Tue, 2009-07-07 at 09:08 +0200, Rails L. wrote:

> Will there be more typeabbrev than the specified 2?. Do you expect more  
> in the future?. If so, how many?
> 
> If typeabbrev are only two for each stockid then why cant you put them  
> in stockmaster itself?.
> 
> Alternatively, you can have a Prices table with multiple columns with  
> each column representing typeabbrev…
> 
> please provide more info…

* * *

this is legacy db from weberp accounting (php) and I can’t do much about  
it but to live with it.

the way this ‘prices’ is setup, typeabbrev field is useful only if you  
have labeled categories, which I presently have 2, Wholesale and Retail  
and maybe will have more in the future. but this file could also have a  
special ‘customer’ price for a matching stock item which would be null  
in typeabbrev.

But the reality is that I can’t change anything here without having to  
tackles some rewrite of the accounting software and that is not going to  
happen.

Thanks

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 7, 2009, 9:08am UTC](https://www.ruby-forum.com/t/multiple-associated-records/171628/5 "2009-07-07T09:08:49Z")

</div>

Will there be more typeabbrev than the specified 2?. Do you expect more  
in the future?. If so, how many?

If typeabbrev are only two for each stockid then why cant you put them  
in stockmaster itself?.

Alternatively, you can have a Prices table with multiple columns with  
each column representing typeabbrev…

please provide more info…

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 8, 2009, 9:10am UTC](https://www.ruby-forum.com/t/multiple-associated-records/171628/6 "2009-07-08T09:10:25Z")

</div>

I think the has one assoc. Is a good idea. But use :class\_name =\>  
‘Price’ instead of :through and :source

2009/7/6, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > has\_many :prices  
> > end  
> > and my issue is that I have both a ‘WH’ and ‘RE’ typeabbrev for each  
> > Stockmaster class),  
> > association “prices” in model Stockmaster  
> > from (irb):6
> 
> >

–  
Von meinen Mobilgerät aus gesendet
