Seb*_*ian 30 ruby ruby-on-rails duplicates has-many has-many-through
我怎样才能实现以下目标?我有两个模型(博客和读者)和一个JOIN表,它允许我在它们之间建立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
Run Code Online (Sandbox Code Playgroud)
我现在想做的是将读者添加到不同的博客中.但是,条件是我只能将博客添加到博客中.因此表中不得有任何重复(相同readerID
,相同blogID
)BlogsReaders
.我怎样才能做到这一点?
第二个问题是,如何获得读者尚未订阅的博客列表(例如,填写下拉选择列表,然后可以将读者添加到另一个博客)?
Ott*_*tto 82
Rails中内置的更简单的解决方案:
class Blog < ActiveRecord::Base
has_many :blogs_readers, :dependent => :destroy
has_many :readers, :through => :blogs_readers, :uniq => true
end
class Reader < ActiveRecord::Base
has_many :blogs_readers, :dependent => :destroy
has_many :blogs, :through => :blogs_readers, :uniq => true
end
class BlogsReaders < ActiveRecord::Base
belongs_to :blog
belongs_to :reader
end
Run Code Online (Sandbox Code Playgroud)
请注意将该:uniq => true
选项添加到has_many
呼叫中.
此外,您可能需要has_and_belongs_to_many
在Blog和Reader之间考虑,除非您在连接模型上有一些其他属性(当前没有这些属性).那种方法也有一个:uniq
选择.
请注意,这不会阻止您在表中创建条目,但它确实在您查询集合时只能获得每个对象中的一个.
更新
在Rails 4中,实现它的方法是通过范围块.以上更改为.
class Blog < ActiveRecord::Base
has_many :blogs_readers, dependent: :destroy
has_many :readers, -> { uniq }, through: :blogs_readers
end
class Reader < ActiveRecord::Base
has_many :blogs_readers, dependent: :destroy
has_many :blogs, -> { uniq }, through: :blogs_readers
end
class BlogsReaders < ActiveRecord::Base
belongs_to :blog
belongs_to :reader
end
Run Code Online (Sandbox Code Playgroud)
Rails的更新5
使用uniq
在范围块将导致错误 NoMethodError: undefined method 'extensions' for []:Array
.distinct
改为使用:
class Blog < ActiveRecord::Base
has_many :blogs_readers, dependent: :destroy
has_many :readers, -> { distinct }, through: :blogs_readers
end
class Reader < ActiveRecord::Base
has_many :blogs_readers, dependent: :destroy
has_many :blogs, -> { distinct }, through: :blogs_readers
end
class BlogsReaders < ActiveRecord::Base
belongs_to :blog
belongs_to :reader
end
Run Code Online (Sandbox Code Playgroud)
Mik*_*een 37
这应该照顾你的第一个问题:
class BlogsReaders < ActiveRecord::Base
belongs_to :blog
belongs_to :reader
validates_uniqueness_of :reader_id, :scope => :blog_id
end
Run Code Online (Sandbox Code Playgroud)
pas*_*llo 17
Rails 5.1方式
class Blog < ActiveRecord::Base
has_many :blogs_readers, dependent: :destroy
has_many :readers, -> { distinct }, through: :blogs_readers
end
class Reader < ActiveRecord::Base
has_many :blogs_readers, dependent: :destroy
has_many :blogs, -> { distinct }, through: :blogs_readers
end
class BlogsReaders < ActiveRecord::Base
belongs_to :blog
belongs_to :reader
end
Run Code Online (Sandbox Code Playgroud)
关于什么:
Blog.find(:all,
:conditions => ['id NOT IN (?)', the_reader.blog_ids])
Run Code Online (Sandbox Code Playgroud)
Rails通过关联方法为我们收集ids!:)
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
归档时间: |
|
查看次数: |
19485 次 |
最近记录: |