0
如何在User上編寫一個範圍,該範圍返回與該用戶關聯的所有地址(通過訂單),其中order.paid == true?如何編寫這個檢查中間表中的條件的Rails作用域?
請注意,Order具有多個具有不同名稱的關聯Address實例,但Address模型是相同的。
class Order < ActiveRecord::Base
belongs_to :shipping_address, :class_name => "Address", :foreign_key => :shipping_address_id
belongs_to :billing_address, :class_name => "Address", :foreign_key => :billing_address_id
belongs_to :user
# the order table has a boolean 'paid' field
end
class Address < ActiveRecord::Base
has_one :order
end
class User < ActiveRecord::Base
has_many :orders
# I want to create a scope here that returns addresses that belong to paid orders
end