Re: [Solved] Best approach to implement model? (Many-to-Many, polymorphic)
Any way I can get the Value from an Item through the property_valuation relation (without the need to query the property_valuation first to get the value)?
You are not logged in. Please login or register.
Rails Forum - Ruby on Rails Help and Discussion Forum » Models and Database » [Solved] Best approach to implement model? (Many-to-Many, polymorphic)
Any way I can get the Value from an Item through the property_valuation relation (without the need to query the property_valuation first to get the value)?
As per my last post either change the has_one to a belongs_to or move the foreign key.
Your problem is that with a parent child db relationship the foreign key belongs on the child. In your case property_valuation has_one valuable which means the table defining the valuable class needs a property_valuation_id.
Ether that or change the has_one to a belongs_to
I already had the foreign key on the valuable side, but can't get the value from a property_valuation. I tried changing has_one/belongs_to but apparently I can't simply change has_one by belongs_to, as I'll get this error:
i.properties.push(p)
ArgumentError: Unknown key: polymorphicI'm assuming the idea was to mantain the rest without changes:
class PropertyValuation < ActiveRecord::Base
attr_accessible :valuable
attr_accessor :valuable
belongs_to :property
belongs_to :item
has_one :valuable, :polymorphic => true
endclass DecimalValue < ActiveRecord::Base
attr_accessible :value
belongs_to :property_valuation, :as => :valuable
endMigrations:
class CreateDecimalValues < ActiveRecord::Migration
def change
create_table :decimal_values do |t|
t.decimal :value
t.references :property_valuation
t.timestamps
end
end
endclass CreatePropertyValuations < ActiveRecord::Migration
def change
create_table :property_valuations do |t|
t.references :item
t.references :property
t.references :valuable, :polymorphic => true
t.timestamps
end
add_index :property_valuations, :item_id
add_index :property_valuations, :property_id
add_index :property_valuations, :valuable_id
end
endLast edited by imnotnot (2012-10-29 15:16:03)
ok. Sorry, I misread your db diagram.
There is somethig really basic and obvious I'm missing but I can't see the wood for the trees.
I'll get back to you
I revised the Railscast for polymorphic associations (railscasts.com/episodes/154-polymorphic-association), but I found my behavior to be different from their example: in my case I'm trying to associate something which has many forms (DecimalValue, TextValue, etc) to a model (PropertyValuation). On the contrary, they are associating a Comment to something which has many forms (Image, Post, etc).
Can this invalidate the use of a polymorphic association in my scenario?
EDIT: Apparently I found many reported issues related to the use of has_one in a polymorphic association. I also found this (http://stackoverflow.com/a/5140580/554972) which seems interesting to try, but didn't work.
Last edited by imnotnot (2012-10-31 06:55:30)
Apparently all I needed to do was to remove the line:
attr_accessor :valuableNow I can create a "valuable" via property_valuation.valuable = ...
Dammit! I knew it was going to be something dead easy
Well spotted. I totally mied that and all the code I was trying worked fine.
Hosting provided by aTech Media