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