Has_many :through with self-referencial AND polymorphism

Hello, despite many readings I still don’t understand how to solve my
problem.

I have Publication, Picture, File classes
A Publication can be related (a one way relation, like a www link) with
another Publication, Picture or File

I tried this :

Publication :
has_many :related_elements, :dependent => :destroy, :foreign_key =>
:publication_id
has_many :related, :through => :related_elements, :as => :linked,
:source => :publication

RelatedElement :
belongs_to :publication

And migrate as
create_table :related_elements do |t|
t.integer :linked_id
t.string :linked_type
t.integer :publication_id
end

When I do
a_publication.related[0] = another_publication
a_publication.save!

I would expect the related_elements table beeing updated with
publication_id = the id of a_publication
linked_id = the id of another_publication
linked_type = the class name ‘Publication’

But nothing happen… And I must confess that I’m pretty sure my
has_many are totally wrong

All this stuff is really too complex for me, so your help will be
greatly appreciated !! Thanks

here you have an example

http://pastie.org/140772

I hope it helps.

2008/1/18, Tom S. [email protected]:

Publication :
t.integer :linked_id
linked_id = the id of another_publication

Pedro Del G.

Email : [email protected]

Pedro Del gallego wrote:

Thanks Pedro, this is a first step in the right direction.
Now I would like to add support for polymorphism, I mean that the item
related could be of different class (this -> related_to -> book, this ->
related_to -> image, and so on)

It seems that it’s not a simple case
(http://blog.hasmanythrough.com/2006/4/3/polymorphic-through)

Does it changed with Rails 2.0 ?

I tried the solution provided on the hasmanythrough blog but without
success.

Should I go to the has_many_polymorphs plugin ??
(http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/README.html)

I don’t like the idea of overusing plugins when I don’t know how to
handle a special situation… It’s kind of black box in that case

Thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs