A Video, a Song and an Article can have many Tags. And each Tag also can have many Video, Songs or Articles. So I have 5 models: Video, Song, Article, Tag and Taggings.
Here are these models:
class Video < ActiveRecord::Base
has_many :tags, :through => :taggings
end
class Song < ActiveRecord::Base
has_many :tags, :through => :taggings
end
class Article < ActiveRecord::Base
has_many :tags, :through => :taggings
end
class Tag < ActiveRecord::Base
has_many :articles
has_many :videos
has_many :songs
belong_to :taggings, :polymorphic => true #is this correct?
end
The database definition of Taggings
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.string "taggable_type"
t.integer "taggable_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
Taggings model:
class Taggings < ActiveRecord::Base
belongs_to :tag #is this correct?
belong_to :taggable, :polymorphic => true #is this correct?
end
The issue I’m worried by is, do I have right definitions of model (belongs_to, has_many?) ? My gut tells me that I missed something. I’ve seen many articles and I’m quite confused by them.
You need these changes:
About
with_options.Your class
Taggingsseems ok except its name. It has to be singular,Tagging: