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)?

Re: [Solved] Best approach to implement model? (Many-to-Many, polymorphic)

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

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: [Solved] Best approach to implement model? (Many-to-Many, polymorphic)

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: polymorphic

I'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
end
class DecimalValue < ActiveRecord::Base
  attr_accessible :value
  
  belongs_to :property_valuation, :as => :valuable
end

Migrations:

class CreateDecimalValues < ActiveRecord::Migration
  def change
    create_table :decimal_values do |t|
      t.decimal :value
      t.references :property_valuation

      t.timestamps
    end
  end
end
class 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
end

Last edited by imnotnot (2012-10-29 15:16:03)

Re: [Solved] Best approach to implement model? (Many-to-Many, polymorphic)

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

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: [Solved] Best approach to implement model? (Many-to-Many, polymorphic)

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)

Re: [Solved] Best approach to implement model? (Many-to-Many, polymorphic)

Apparently all I needed to do was to remove the line:

attr_accessor :valuable

Now I can create a "valuable" via property_valuation.valuable = ...

Re: [Solved] Best approach to implement model? (Many-to-Many, polymorphic)

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.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)