has_one关联的未知列错误并在命名作用域中加入

KJF*_*KJF 1 activerecord ruby-on-rails

我有一个产品和payment_notification模型与以下关联

class Product < ActiveRecord::Base
  has_one :payment_notification
end

class PaymentNotification < ActiveRecord::Base
  belongs_to :product
end
Run Code Online (Sandbox Code Playgroud)

我正在设置一个命名范围,该范围应该获取其关联的payment_notification已完成状态的所有产品.

我觉得这应该适用于我的产品型号:

scope :completed, joins(:payment_notification).where(:payment_notification => { :status => 'Completed' })
Run Code Online (Sandbox Code Playgroud)

但是这会导致以下错误:

Error: Mysql::Error: Unknown column 'payment_notification.status' in 'where clause': SELECT     `products`.* FROM       `items`  INNER JOIN `payment_notifications` ON `payment_notifications`.`product_id` = `productss`.`id` WHERE     (`payment_notification`.`status` = 'Completed')
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Har*_*tty 6

试试这个:

scope :completed, joins(:payment_notification).where(:payment_notifications => 
                    { :status => 'Completed' })
Run Code Online (Sandbox Code Playgroud)

注意复数:payment_notifications.