Search code examples
scopeassociationsruby-on-rails-3.1drymodel-associations

Scope association in rails 3.1


Something very convenient we used to use in rails 3.0 is the scope reusability. Lets see an example.

class Wheel < AR::B
  belongs_to :car
  scope :deflated, where (:deflated => true)
end

class Car < AR::B
  has_many :wheels
  scope :out_of_service, joins(:wheels) & Wheel.deflated
end

But it does not seems to work anymore in 3.1, do you know if there is a new way to do that ? Thanks


Solution

  • My feedback on this, it does work, not sure where my problem was coming from.

    You can also use the merge() function, & is a shortcut.

    scope :out_of_service, joins(:wheels).merge(Wheel.deflated)
    

    Cheers