Joe C. wrote:
Hi, lets say I have a a table, “Linkings” that I am using for
polymorphic associations. One of the associations is to the “person”
table, and there are several other associations.
“Person” has 2 types, “slacker” and “suckup”.
It is modelled as such:
class Linking < ActiveRecord::Base
belongs_to :linkable, :polymorphic => true
class Person < ActiveRecord::Base
class Slacker < Person
class Yadda < Person
I’d like to have one controller for both “slacker” and “suckup”, so I
tried using this:
If the linkable type is a “Person” I get either “slacker” or “suckup”,
the type of “person”. Is there a way to get the actual linkable type?
I’m not sure exactly what you’re trying to do, since you left out the
table schemas/migrations. I guess you’re confusing STI and polymorphic
associations. STI lets you use inheritance to put subclasses in the same
table as their common superclass. Polymorphic associations use a
composite key to allow a model to be associated by models in several
other tables. I don’t think you need to combine the two for what you’re
If you’re doing a polymorphic has_many in Person, it should go like
has_many :linkings, :as => :linkable
But you don’t need to do that since the
belongs_to :person declaration
works for slackers and yaddas too.
Drop the polymorphism, just use STI. Does that help?