我有一個與購買模型有很多關係的簡單客戶模型。使用聯接(Rails)指定has_many關聯的默認排序?
class Customer < ActiveRecord::Base
has_many :purchases
end
我多次發現,我需要在我在下面的意見的方式訂購Customer.purchases:
@customer.purchases.joins(:shop).order("shops.position").order(:position) #yes, two orders chained
在保持事物DRY的利益,我希望把這個地方集中,所以我不必反覆做。理想情況下,我希望將其作爲Customer.purchases的默認排序。例如:
class Customer < ActiveRecord::Base
has_many :purchases, :order => joins(:shop).order("shops.position").order(:position)
end
顯然上述不起作用。我應該怎麼做?
似乎沒有工作。給我一個'未定義的方法'鍵?'爲零:NilClass'錯誤。 –
請使用:joins => [:shop] – Mohanraj
不可以。仍然有同樣的錯誤... –