如何与标签,用户和文章建立多态关联?

时间:2013-06-24 22:10:07

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

我正在创建一个Tag系统,我希望用户能够创建可以包含描述的标签。标签属于用户,可以单独创建,也可以与文章一起创建。我希望能够执行Tag.usersTag.articles之类的操作来查看属于标记的所有文章或用户。以下是我到目前为止的情况:

class User < ActiveRecord::Base
  attr_accessible # Devise
  has_many :taggings
  has_many :articles
  has_many :tags

  # Table - using Devise
end

class Tag < ActiveRecord::Base
  attr_accessible :name, :description
  has_many :taggings
  has_many :articles, through: :taggings
  belongs_to :article

   # Table - user_id, article_id, name, description
end

class Article < ActiveRecord::Base
  attr_accessible :name, :content, :published_on
  has_many :taggings
  has_many :tags, through: :taggings
  belongs_to :user

  # Table - user_id, name, content, published_on
end

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :article
  belongs_to :user

 # Table - user_id, tag_id, article_id
end

这是做我想要的正确方法吗?

必须访问哪些属性以及哪些关联?

0 个答案:

没有答案