我正在開發一個像stackoverflow這樣的應用程序,問題或文章只有一個標籤。一個標籤必須有一個或多個文章。RoR中這兩個表之間的關係是什麼?
因此,我在RoR中進行遷移。我認爲哪種關係適合這兩種表格。在文章表中,應該使用「has_many」,並且在標籤表中,應該使用「has_many」。 但我的想法是,有必要增加一個表在中間,像....
所以,第一個是這樣的:
class Article < ActiveRecord::Base
has_many :tags
end
class Tag < ActiveRecord::Base
has_many :articles
end
或者是這樣的:
class Article < ActiveRecord::Base
has_many :articleTagList
has_many :tags, :through => : articleTagLists
end
class Tag < ActiveRecord::Base
has_many :articleTagList
has_many :articles, :through => :articleTagLists
end
class ArticleTagList < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
我可以建議使用http://github.com/mbleigh/acts-as-taggable-on嗎? – davidcelis 2010-07-20 18:10:43