書籍およびムービーのモデルのみが次のように異なります。多形多対多
例1:Book has_many :taggings, :as => :taggable
例2:Book has_many :taggings, :through => :taggable
この違いは何を意味するのでしょうか?
class Book < ActiveRecord::Base
has_many :taggings, :through => :taggable
has_many :tags, :through => :taggings
end
class Movie < ActiveRecord::Base
has_many :taggings, :through => :taggable
has_many :tags, :through => :taggings
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :books, :through => :taggings, :source => :taggable, :source_type => "Book"
has_many :movies, :through => :taggings, :source => :taggable, :source_type => "Movie"
end
class Tagging < ActiveRecord::Base
belongs_to :taggable, :polymorphic => true
belongs_to :tag
end
ありがとうございます。私はまた、2番目のケースがうまくいくかどうかはわかりません。私はこれらのコード例をブログ記事で見つけました。第2の記事では、 ":through =>:taggable"を ":as =>:taggable"に変更するというコメントがあります。おそらく、Taggableクラスがないとうまくいかないでしょう。 –