b_d
b_d

Reputation: 787

Separate Polymorphic Association table

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.

Upvotes: 3

Views: 851

Answers (1)

C Del
C Del

Reputation: 53

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

Upvotes: 1

Related Questions