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.
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.
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.
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.
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…
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. [email protected]:
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