Given this class:
class MyModel < ActiveRecord::Base
belongs_to :association1
belongs_to :association2, :polymorphic => true
end
I know that when I set association1, it sets association1_id to the ID of object 1
m = MyModel.new
m.association1 = object1
#<MyModel id: nil, association1_id: 1, association2_id: nil, association2_type: nil>
I know that when I set association2, it sets association2_id AND association2_type
m.association2 = object2
#<MyModel id: nil, association1_id: 1, association2_id: 2, association2_type: 'ClassType'>
My question is:
Is there a function that can easily tell me what information is being set on an object in hash form?
MyModel.magic_function(:association1, object1)
# returns {:association1_id => 1}
MyModel.magic_function(:association2, object2)
# returns {:association2_id => 2, :association2_type => 'ClassType'}
This is the stop gap solution I have for now, just though I’d share:
Is this built into rails somewhere?