【问题标题】:Scope but error message ArgumentError: tried to create Proc object without a block范围但错误消息 ArgumentError: 试图创建没有块的 Proc 对象
【发布时间】:2021-01-01 20:17:43
【问题描述】:

我正在尝试创建一个范围,以找出所有地址为 0 的联系人。 Got error message ArgumentError: tried to create Proc object without a block 在 rails c 中运行命令“Contact.noaddress”时。

这是我的联系模型,包括范围:

class Contact < ActiveRecord::Base
  attr_accessible :email, :firstname, :lastname, :mobilephone, :fullname
  has_many :addresses
  validates_presence_of :firstname, :lastname


   scope :noaddressed, lambda do |addresses|
    joins(:addresses).where('addresses.created_at.empty?', true)
   end
end

这里是地址模型

class Address < ActiveRecord::Base
  attr_accessible :city, :country, :postalcode, :region, :street
  belongs_to :contact
end

有人可以帮帮我吗?

【问题讨论】:

    标签: ruby-on-rails-3 scope


    【解决方案1】:

    这听起来像是引用了here 的(优先级问题?)。

    如果您将范围更改为:

    scope :noaddresses, (lambda do
      joins(:addresses).where('addresses.created_at is null')
    end)
    

    scope :noaddresses, lambda { joins(:addresses).where('addresses.created_at is null') }
    

    另外,我看不出你在哪里使用块参数|addresses|。你忘记使用了吗?否则,您可以移除它及其周围的管道。

    更新:我删除了 |addresses| 参数并将查询更新为有效的 SQL 语法。

    【讨论】:

    • 非常感谢您的帮助。但得到错误'ArgumentError:参数数量错误(0代表1)'即使我使用了named_scope
    • addresses 参数需要在 noaddresses(addresses_arguement) 中提供或删除。
    猜你喜欢
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2013-01-15
    • 2018-05-21
    相关资源
    最近更新 更多