范围主体需要是可调用的Rails 4

时间:2015-09-23 16:41:28

标签: ruby-on-rails ruby-on-rails-4

我在rails 4中创建了一个范围:

scope :available, {conditions: ["`model_name`.attribute NOT LIKE ?", "%**"]}

但我一直收到错误消息The scope body needs to be callable

我也试过了:

scope :available, -> where ["'model_name'.attribute NOT LIKE ?", "%**"]

但它在这里说语法错误。那么rails 4中的语法是什么,或者首先如何解决错误?

1 个答案:

答案 0 :(得分:2)

我已经设法快速解决了,这是解决方案:

scope :available, -> (attribute) { where("'model'.attribute NOT LIKE ?", "%**") }

链接到此链接:http://www.justinweiss.com/articles/search-and-filter-rails-models-without-bloating-your-controller/