gpt4 book ai didi

ruby-on-rails-3 - 范围但错误消息 ArgumentError : tried to create Proc object without a block

转载 作者:行者123 更新时间:2023-12-04 10:51:04 26 4
gpt4 key购买 nike

我正在尝试创建一个范围,找出所有地址为 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
有人可以帮我吗?

最佳答案

听起来像是(优先级问题?)引用 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 语法。

关于ruby-on-rails-3 - 范围但错误消息 ArgumentError : tried to create Proc object without a block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16016668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com