Object class with STI and Polymorphic Assoc's


#1

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 :person
belongs_to :linkable, :polymorphic => true
end

class Person < ActiveRecord::Base
has_many :linkables
end
class Slacker < Person
end
class Yadda < Person
end

I’d like to have one controller for both “slacker” and “suckup”, so I
tried using this:
@linkable.class

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?


#2

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 :person
belongs_to :linkable, :polymorphic => true
end

class Person < ActiveRecord::Base
has_many :linkables
end
class Slacker < Person
end
class Yadda < Person
end

I’d like to have one controller for both “slacker” and “suckup”, so I
tried using this:
@linkable.class

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
doing.

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?


Josh S.
http://blog.hasmanythrough.com


#3

On 15 May 2006, at 08:01, Josh S. wrote:

belongs_to :linkable, :polymorphic => true
I’d like to have one controller for both “slacker” and “suckup”, so I
associations. STI lets you use inheritance to put subclasses in the
declaration
works for slackers and yaddas too.

Drop the polymorphism, just use STI. Does that help?

I believe what he wants is the following:
He has a table people and a table companies and a table projects for
example. A person can be linked to a person, or a company, or a
project. A company can be linked to a company, a person or a project.
A project can be linked to another project, a company or a person. So
basically, you can link anything to anything. In this case, I would
go for a normal many to many relationship (for the self referential
one, you can use Rails Recipe n° 18), overriding the defaults of the
habtm relationship to use that one table which holds the links.

Best regards

Peter De Berdt