全て!追加の has_many リレーションを作成して、必要な列のみを選択したい
例
class Price < ActiveRecord::Base
self.table_name = "pricelist_prices"
has_many :order_items, :primary_key=> :city_id, :foreign_key=> :city_id
has_many :orders, :through => :order_items
end
だからそれは今動作します。しかし、:orders のように機能する関連付けを作成したいのですが、:select オプションがあります。
例
has_many :orders, :through => :order_items, :select=>"price"
しかし、現在の :orders accociation をオーバーライドしたくありません。これを行う方法?
ソースオプションを使用したUPDアゾトショーの例!大丈夫ですが、この付属物を includes で使用すると機能しません。
Price.where(:id=>[12759,12758]).includes(:prices_from_orders)
(Object doesn't support #inspect)
Price.where(:id=>[12759,12758]).includes(:prices_from_orders).first
NoMethodError: undefined method `each' for nil:NilClass
from /Users/igorfedoronchuk/.rvm/gems/ruby-1.9.2-p180@new/gems/activerecord-3.2.1/lib/active_record/associations/preloader/association.rb:88:in `block in associated_records_by_owner'
UPD2
インクルード メソッドでそのような関連付けを使用する場合、primary_key の選択も追加すると、AR は誰がレコードの所有者であるかがわからないという問題に気付きました。
has_many :orders, :through => :order_items, :select=>"id, price"