1

在 Rails 3.2 中,我有一个用户模型如下->

User model

has_many :billing_invoices, :through => :user_purchases, :select => "DISTINCT billing_invoices.invoice_date,billing_invoices.account_number"

我正在尝试升级到 Rails 4.0,但我收到了弃用警告,改为使用范围块。如何重写这个 has_many 语句以在 4.0 中工作?

4

1 回答 1

2

我认为这是您需要的:

has_many :billing_invoices, -> { distinct }, through: :user_purchases

请参阅https://guides.rubyonrails.org/association_basics.html#scopes-for-has-many-distinct

更新:

如果你想覆盖SELECT然后:

has_many :billing_invoices, -> { select("DISTINCT billing_invoices.invoice_date,billing_invoices.account_number") }, :through => :user_purchases

请参阅:https ://guides.rubyonrails.org/association_basics.html#scopes-for-has-many-select

于 2018-11-26T22:57:55.263 回答