0

I've got code similar to

class Article < ActiveRecord::Base
  has_many :comments
  scope :with_comments, joins(:comments)
end

based on this answer, but when I use it in Rails 4.2, I get

/Users/agrimm/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/scoping/named.rb:143:in `scope': The scope body needs to be callable. (ArgumentError)

Have the rules with regards to allowing joins in scope changed between Rails 4 and Rails 4.2? I can see mention of joins in the 4.2 release notes, but I can't tell if it's applicable here.

Community
  • 1
  • 1
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338

3 Answers3

3

The scope body needs to be callable. (ArgumentError)

This should work

scope :with_comments, -> { joins(:comments) }

A nice explanation here

Community
  • 1
  • 1
Pavan
  • 33,316
  • 7
  • 50
  • 76
2

Can you try it like this ?

scope :with_comments, -> { joins(:comments)}
2

Your syntax is wrong. You should do as follows:

scope :with_comments, -> { joins(:comments) }

You should have a look at this documentation

abhinavmsra
  • 666
  • 6
  • 21