Ruby on rails 如何锁定型号';s关联的集合?
我有 有一个地方,我想用一个锁定的select来打开所有的小部件。因此,我想做如下等效工作:Ruby on rails 如何锁定型号';s关联的集合?,ruby-on-rails,locking,associations,Ruby On Rails,Locking,Associations,我有 有一个地方,我想用一个锁定的select来打开所有的小部件。因此,我想做如下等效工作: class Foo has_many :widgets end 使用更好的代码,例如: @widgets_to_work_with = Widget.find_all_by_foo_id(@foo.id, :lock => true) 最好的方法是什么?您可以在Foo ActiveRecord中重新定义方法小部件,或者更安全地添加另一个方法 a、 e 希望是有用的 @widgets_to_
class Foo
has_many :widgets
end
使用更好的代码,例如:
@widgets_to_work_with = Widget.find_all_by_foo_id(@foo.id, :lock => true)
最好的方法是什么?您可以在Foo ActiveRecord中重新定义方法小部件,或者更安全地添加另一个方法 a、 e 希望是有用的
@widgets_to_work_with = @foo.widgets(:lock => true)
# in Foo.rb
#...
def self.locked_widgets
Widget.find_all_by_foo_id(self.id, :lock => true)
end