Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument Error: The scope body needs to be callable

I'm working through the 'Ruby On Rails 3 Essential Training' and have received a problem when using name scopes. When finding records and using queries withing the Rails console everything went smoothly until I tried to use a name scope in my subject.rb file. This is my code in the subject.rb file.

Class Subject < ActiveRecord::Base    scope :visible, where(:visible => true)  end    

I saved the .rb file and restarted my Rails console but when I run from my rails console:

subjects = Subject.visible 

I get: ArgumentError: The scope body needs to be callable.

Does anyone know why I'm getting this error.

like image 421
Sam Gruse Avatar asked Mar 09 '15 21:03

Sam Gruse


2 Answers

The scope's body needs to be wrapped in something callable like a Proc or Lambda:

scope :visible, -> {   where(:visible => true) } 

The reason for this is that it ensures the contents of the block is evaluated each time the scope is used.

like image 200
infused Avatar answered Oct 04 '22 14:10

infused


I got the same error , while before my solution I had a space between where and ( like below

scope :registered , -> { where ( place_id:  :place_id , is_registered: :true ) } 

after i removed the space between where and ( like below i made my page working

scope :registered , -> { where( place_id:  :place_id , is_registered: :true ) } 
like image 35
Mani Avatar answered Oct 04 '22 14:10

Mani