【发布时间】:2011-01-10 12:30:29
【问题描述】:
假设我与附加条件有以下关联:
belongs_to :admin_user,
:class_name => 'User',
:foreign_key => :admin_user_id,
:conditions=> 'users.admin=TRUE' # or any variation with hash or array, {:admin => true}, etc.
belongs_to 上的 :conditions 选项的API doc states 将:
指定条件 关联对象必须按顺序相遇 包含在 WHERE SQL 中 片段,比如authorized = 1。
但是输出在 select 上没有显示 WHERE 子句,并且在任何情况下,我都希望在 belongs_to 上这样的条件会阻止在 INSERT 而不是 SELECT 上保持这种关系。这个选项似乎对belongs_to 关联没有影响,除非我遗漏了一些东西。该选项在 has_many 上是有意义的,我只是看不出它如何应用于 belongs_to。
编辑:进一步的研究表明,您确实可以保留违反条件的关联,但您无法在重新加载记录后检索关联的记录。
在这样定义的类上:
class Widget < ActiveRecord::Base
belongs_to :big_bloop,
:class_name => "Bloop",
:foreign_key => :big_bloop_id,
:conditions => ["big_bloop = ?", true]
belongs_to :bloop, :conditions => ["big_bloop = ?", true]
end
...从控制台我们看到:
>> bloop = Bloop.new
=> #<Bloop id: nil, name: nil, big_bloop: nil>
>> widget = Widget.new
=> #<Widget id: nil, name: nil, bloop_id: nil, big_bloop_id: nil>
>> widget.bloop = bloop
=> #<Bloop id: nil, name: nil, big_bloop: nil>
>> widget.save!
=> true
>> widget
=> #<Widget id: 2, name: nil, bloop_id: 2, big_bloop_id: nil>
我已经关联了一个违反条件的 bloop 并保存了它。关联被持久化到数据库(参见上面最后一行的 bloop_id 和 big_bloop_id)。
>> big_bloop = Bloop.new
=> #<Bloop id: nil, name: nil, big_bloop: nil>
>> widget.big_bloop = big_bloop
=> #<Bloop id: nil, name: nil, big_bloop: nil>
>> widget.save!
=> true
>> widget
=> #<Widget id: 2, name: nil, bloop_id: 2, big_bloop_id: 3>
相同的东西,不同的属性。
>> widget.bloop
=> #<Bloop id: 2, name: nil, big_bloop: nil>
>> widget.big_bloop
=> #<Bloop id: 3, name: nil, big_bloop: nil>
两个无效的 bloop 都保留在内存中。
>> widget.reload
=> #<Widget id: 2, name: nil, bloop_id: 2, big_bloop_id: 3>
>> widget.bloop
=> nil
>> widget.big_bloop
=> nil
重新加载后,它们就消失了,因为 SELECT 语句确实使用了 WHERE 子句来排除它们。
Bloop Load (0.3ms) SELECT * FROM `bloops` WHERE (`bloops`.`id` = 2 AND (big_bloop = 1))
然而小部件仍然有引用:
>> widget
=> #<Widget id: 2, name: nil, bloop_id: 2, big_bloop_id: 3>
对我来说似乎很奇怪,但你去吧。
【问题讨论】:
标签: ruby-on-rails activerecord