开发者

How can I lock a model's associated collection?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜