Search code examples
ruby-on-railsruby-on-rails-4ruby-on-rails-3.2rails-3-upgrade

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?


Solution

  • 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