Rails 将 3.2 升级到 4.0:模型弃用警告

Rails Upgrade 3.2 to 4.0: Model deprecation warning

在Rails3.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 中工作?

我想这就是你需要的:

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