【问题标题】:Rails Upgrade 3.2 to 4.0: Model deprecation warningRails 升级 3.2 到 4.0:模型弃用警告
【发布时间】:2019-04-28 15:15:50
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-3.2 rails-3-upgrade


    【解决方案1】:

    我想这就是你需要的:

    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

    【讨论】:

    • 谢谢!如何针对多个不同的值进行调整?
    • 我正在尝试 has_many :billing_invoices, -> { select "DISTINCT billing_invoices.invoice_date,billing_invoices.account_number"}, :through => :user_purchases
    猜你喜欢
    • 2023-03-06
    • 2015-02-16
    • 2019-03-05
    • 2012-08-02
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多