KavitaC
KavitaC

Reputation: 635

Rails Upgrade 3.2 to 4.0: Model deprecation warning

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?

Upvotes: 1

Views: 57

Answers (1)

kasperite
kasperite

Reputation: 2478

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

Upvotes: 2

Related Questions