加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 从rails 3升级到rails 4时has_many关联的错误顺序

发布时间:2020-12-17 03:16:53 所属栏目:百科 来源:网络整理
导读:我正在尝试将一个项目从Rails 3更新到Rails 4.在Rails 3中我做了: class Sale ActiveRecord::Base has_many :windows,:dependent = :destroy has_many :tint_codes,:through = :windows,:uniq = true,:order = 'code ASC' has_many :tint_types,:through =
我正在尝试将一个项目从Rails 3更新到Rails 4.在Rails 3中我做了:

class Sale < ActiveRecord::Base
  has_many :windows,:dependent => :destroy
  has_many :tint_codes,:through => :windows,:uniq => true,:order => 'code ASC'
  has_many :tint_types,:through => :tint_codes,:order => 'value ASC'
end

当我调用sale.tint_types时,它在Rails 3中执行以下查询:

SELECT DISTINCT "tint_types".* FROM "tint_types" INNER JOIN "tint_codes" ON "tint_types"."id" = "tint_codes"."tint_type_id" INNER JOIN "windows" ON "tint_codes"."id" = "windows"."tint_code_id" WHERE "windows"."sale_id" = 2 ORDER BY value ASC

我为Rails 4更新了这样的:

class Sale < ActiveRecord::Base
  has_many :windows,-> { order('code').uniq },:through => :windows
  has_many :tint_types,-> { order('value').uniq },:through => :tint_codes
end

查询更改为:

SELECT DISTINCT "tint_types".* FROM "tint_types" INNER JOIN "tint_codes" ON "tint_types"."id" = "tint_codes"."tint_type_id" INNER JOIN "windows" ON "tint_codes"."id" = "windows"."tint_code_id" WHERE "windows"."sale_id" = $1  ORDER BY value,code

它在order子句中添加了代码,这使得PostgreSQL通过错误.我认为这是因为范围,但我无法弄清楚如何获得ORDER BY代码.

任何帮助表示赞赏,
谢谢!

解决方法

Rails社区帮助我找到了解决方案.

class Sale < ActiveRecord::Base
  has_many :windows,-> { uniq },:through => :tint_codes

  def tint_types
    super.reorder(nil).order(:width => :asc)
  end
end

有关详细信息,请参阅https://github.com/rails/rails/issues/12719.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读