本文介绍了Rails:named_scope,lambda和blocks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为以下两个是等效的:

I thought the following two were equivalent:

named_scope :admin, lambda { |company_id| {:conditions => ['company_id = ?', company_id]} }

named_scope :admin, lambda do |company_id|
  {:conditions => ['company_id = ?', company_id]}
end

但是Ruby在抱怨:

ArgumentError: tried to create Proc object without a block

有什么想法吗?

推荐答案

这是解析器问题.试试这个

it's a parser problem. try this

named_scope :admin, (lambda do |company_id|
  {:conditions => ['company_id = ?', company_id]}
end)

这篇关于Rails:named_scope,lambda和blocks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:20