Forum: Ruby on Rails object class with STI and Polymorphic Assoc's.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe C. (Guest)
on 2006-05-14 22:05
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?
Josh S. (Guest)
on 2006-05-15 10:01
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
Peter De Berdt (Guest)
on 2006-05-15 11:36
(Received via mailing list)
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
This topic is locked and can not be replied to.