multiple associated records

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

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?

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.

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...

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