Argument Error: The scope body needs to be callable

前端 未结 4 1052
南方客
南方客 2020-12-24 01:24

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

4条回答
  •  滥情空心
    2020-12-24 02:03

    What you are using: scope :visible, where(:visible => true) goes for eager loading, and has been deprecated in Rails 4.

    scope :visible, where(:visible => true)
    

    This line of code gets evaluated when the particular class is loaded, and not at the time, this very scope is called.

    There are a few cases when this thing does matter, like:

    scope :future, where('published_at > ?', Time.now)
    scope :future, -> { where('published_at > ?', Time.now) }
    

    In first case, ? will be replaced with the very time the class would have been loaded, but the second & correct case, that time will be used at which the scope would have been called on the class.

提交回复
热议问题