Rails Upgrade 3.2 to 4.0: Model deprecation warning

65 Views Asked by At

In Rails 3.2, I have a user Model as follows->

User model

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

I am trying to upgrade to Rails 4.0 and I get the Deprecation warning to use a scope block instead. How can I rewrite this has_many statement to work in 4.0?

1

There are 1 best solutions below

2
On BEST ANSWER

I reckon this is what you need:

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

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

Updated:

If you want to override SELECT then:

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

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