2011-11-12 31 views
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 

回答

0

請糾正我,如果我錯了,但如果添加了

:has_many :addresses 

類訂單,我覺得***它應該給你回所有與該用戶的軌道。我只是在rails上學習ruby,所以如果這是錯的道歉。

相關問題