I think the has one assoc. Is a good idea. But use :class_name =>
'Price' instead of :through and :source

2009/7/6, Craig White <[email protected]>:
>
> On Mon, 2009-07-06 at 11:16 -0700, Craig White wrote:
>> 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?
> ----
> 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.
>
>
> >
>

-- 
Von meinen Mobilgerät aus gesendet

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to