Class Item
has_many :prices
has_one :current_price
end
Class Price
belongs_to :item
#date the price was set attribute
#price attribute
end
「current_price」フィールドで現在の価格(特定のアイテムのmax()日付を保持する価格)を含む(熱心にロードされた)すべてのアイテムを見つけるにはどうすればよいですか?
例えば:
テーブルアイテム:
id = 1 | name = "the_hobbit"
テーブル価格:
id = 1 | item_id = 1 | 価格=10.99$ | 日付=2010-01-01
id = 2 | item_id = 1 | 価格=12.59$ | 日付=2009-04-23
id = 3 | item_id = 1 | 価格=19.99$ | 日付=2013-01-03
@item = Items.find(1)
@item.current_price # should print "19.99$"
編集:私の問題は「グループあたり最大のn 」と呼ばれていると思いますが、「has_one」アソシエーションでこれを適切に行う方法を理解できません...