开发者

How can I lock a model's associated collection?

开发者 https://www.devze.com 2023-02-06 19:36 出处:网络
I have class Foo has_many :widgets end There\'s a place where I want to pull up all the widgets with a locked select. So, I want to do the equivalent of:

I have

class Foo
  has_many :widgets
end

There's a place where I want to pull up all the widgets with a locked select. So, I want to do the equivalent of:

@widgets_to_work_with = Widget.find_all_by_foo_id(@foo.id, :lock =>开发者_JAVA技巧 true)

With nicer code, something like:

@widgets_to_work_with = @foo.widgets(:lock => true)

What's the best way to do this?


you could redefine the method widgets in Foo ActiveRecord or , safer ,add another method a.e.

# in Foo.rb
#...
def self.locked_widgets
  Widget.find_all_by_foo_id(self.id, :lock => true)
end

hope could be usefull

0

精彩评论

暂无评论...
验证码 换一张
取 消