This project is archived and is in readonly mode.
Single Table Inheritance, Polymorphic association (self-referential and normal) bug
-
Iain Adams
Am I right in thinking that the API documentation is wrong on this issue as well.
It claims that having a polymorphic_type=(sType) method should work, but it won't work for me.
From the API......
class Asset < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true def attachable_type=(sType) raise "Setting type" endend
class Post < ActiveRecord::Base
# because we store "Post" in attachable_type now :dependent => :destroy will work has_many :assets, :as => :attachable, :dependent => :destroyend
class GuestPost < Post end
class MemberPost < Post end
The method attachable_type=(sType) is never even used otherwise I would get an error....right?
-
Matt Jones
A couple things:
-
does the "bad SQL" get the right answer?
-
in this case, you don't actually need a polymorphic association. If the models you're associating are all STI subclasses, you can just use regular has_many and friends. The polymorphic stuff comes in where you have objects in the association that aren't even part of the same DB table.
-
-
PommyTom
I'm also getting this error. For the exact same reasoning as the poster.
Forcibly overriding the _type field on the relationship to still have access to the has_many/named_scopes
-
Matt Jones
- State changed from new to wontfix
This is not a bug - the _type field essentially points to the base class. Note that the two statements below are equivalent:
Feeling.find(14) # => PersonalFeeling id 14 PersonalFeeling.find(14) # => PersonalFeeling id 14Closing for now - if anyone can write a test where the existing code returns incorrect results (in other words, finds the wrong records) we can re-examine this.