3

After reading the Ruby on Rails guides and a few of the stackoverflow responses to questions about polymorphic association I understand its use and implementation but I have a question about a specific use scenario. I have tags that can be associated with multiple topics, categories, images and other various models (which also have varying tags) but instead of placing the reference fields (foreign_id, foreign_type) within the tags table, I'd prefer to create a separate association table. Is this still possible using :polymorphic => true?

Something like this:

create_table :tags do |t|
  t.string :name
  t.remove_timestamps
end

create_table :object_tags, :id => false do |t|
  t.integer :tag_id
  t.references :tagable, :polymorphic => true
  t.remove_timestamps
end

If this isn't possible, I was planning on creating the same :object_tags table and using :conditions within the Tag model and other models to force the associations. Is there a rails way of doing this? Thanks! (working with rails 3.0.9 & ruby 1.8.7 <- because deployment server is still using 1.8.7)

UPDATE: Thanks Delba! Answer is a working solution for HABTM polymorphism.

class Tag < ActiveRecord::Base
  has_many :labels
end

class Label < ActiveRecord::Base
  belongs_to :taggable, :polymorphic => true
  belongs_to :tag
end

class Topic < ActiveRecord::Base
  has_many :labels, :as => :taggable
  has_many :tags, :through => :labels
end

create_table :tags, :timestamps => false do |t|
  t.string :name
end

create_table :labels, :timestamps => false, :id => false do |t|
  t.integer :tag_id
  t.references :taggable, :polymorphic => true
end

UPDATE: Because I need bi-directional HABTM, I ended up going back to creating individual tables.

Community
  • 1
  • 1
b_d
  • 777
  • 2
  • 7
  • 18
  • Why do you want to go this way? (pure curiosity, no judgement) – Damien Dec 07 '11 at 21:42
  • It seems more efficient to have a single table with all the associations available for a `Tag` instead of creating separate tables for each Model's association. Currently I do have it setup with individual association tables though. Answering your question I'm realizing there really isn't a need for this kind of solution. The kind of setup that I'm looking for would use more memory though right? – b_d Dec 07 '11 at 22:00
  • 1
    I don't have even the beginning of an opinion for now. I don't think there is a rails way of doing a polymorphic habtm but I will for sure give it a try tomorrow. Great question. Upvoted! Edit: it may to your question but I'm not sure with the answer: http://stackoverflow.com/questions/6964678/habtm-polymorphic-relationship – Damien Dec 07 '11 at 22:30
  • @Delba Thank you for the link. I tested out the solution provided in the link and it works and the solution makes sense. – b_d Dec 07 '11 at 23:42

1 Answers1

1

Yes, and from your description you couldn't have the tagable columns on your tag anyhow since they can have multiple tagable things and vice versa . You mentioned HABT, but you can't do anything like has_and_belongs_to, :polymorphic => true as far as I know.

create_table :object_tags, :id => false do |t|
  t.integer :tag_id
  t.integer :tagable_id
  t.string  :tagable_type
end

Your other tables don't need any columns for object_tags, tags, or tagable.

class Tag < ActiveRecord::Base
  has_many :object_tags
end

class ObjectTag < ActiveRecord::Base
  belongs_to :tagable, :polymorphic => true
  belongs_to :tag
end

class Topic < ActiveRecord::Base
  has_many :object_tags, :as => :tagable
  has_many :tags, :through => :object_tags
end
C Del
  • 53
  • 6