によって設定されているものを決定しますRailsの - オブジェクトのプロパティは、このクラスを考えるとセッター
class MyModel < ActiveRecord::Base
belongs_to :association1
belongs_to :association2, :polymorphic => true
end
私はassociation1を設定すると、それはオブジェクトのIDにassociation1_idを設定していることを知っている1
m = MyModel.new
m.association1 = object1
#<MyModel id: nil, association1_id: 1, association2_id: nil, association2_type: nil>
私は私の質問があり、私はassociation2を設定すると、それはassociation2_idとassociation2_type
m.association2 = object2
#<MyModel id: nil, association1_id: 1, association2_id: 2, association2_type: 'ClassType'>
を設定していることを知っている:
ハッシュ形式のオブジェクトにどのような情報が設定されているかを簡単に知る関数はありますか?
MyModel.magic_function(:association1, object1)
# returns {:association1_id => 1}
MyModel.magic_function(:association2, object2)
# returns {:association2_id => 2, :association2_type => 'ClassType'}
これはおそらく、あなたの質問に編集ではなく、解答されている必要があります。 –