John Bachir
John Bachir

Reputation: 22731

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 => true)

With nicer code, something like:

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

What's the best way to do this?

Upvotes: 0

Views: 176

Answers (1)

andrea
andrea

Reputation: 3505

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

Upvotes: 1

Related Questions