Tag: duplicates

如何避免has_many:through关系中的重复?

我怎样才能实现以下目标? 我有两个模型(博客和读者)和一个JOIN表,它允许我在它们之间建立N:M关系: class Blog :destroy has_many :readers, :through => :blogs_readers end class Reader :destroy has_many :blogs, :through => :blogs_readers end class BlogsReaders < ActiveRecord::Base belongs_to :blog belongs_to :reader end 我现在想做的是将读者添加到不同的博客中。 但是,条件是我只能将博客添加到博客中。 因此, BlogsReaders表中不得有任何重复项(相同的readerID ,相同的blogID )。 我怎样才能做到这一点? 第二个问题是,如何获得读者尚未订阅的博客列表(例如,填写下拉选择列表,然后可以将读者添加到另一个博客)?