【问题标题】:DEPRECATION WARNING: implicit join references. Rails don't want to be a SQL parser for includes弃用警告:隐式连接引用。 Rails 不想成为包含的 SQL 解析器
【发布时间】:2017-11-27 16:29:47
【问题描述】:

我正在将旧应用程序从 rails3 升级到 rails 4。目前在 rails 4.0

在运行 rspec 时,我有很多这样的弃用警告:

  Currently, Active Record recognizes the table in the string, and 
knows to JOIN the comments table to the query, rather than loading 
comments in a separate query. However, doing this without writing a 
full-blown SQL parser is inherently flawed. Since we don't want to 
write an SQL parser, we are removing this functionality. From now on, 
you must explicitly tell Active Record when you are referencing a 
table from a string:

Post.includes(:comments).where("comments.title = 
'foo'").references(:comments)

If you don't rely on implicit join references you can disable the 
feature entirely by setting 
`config.active_record.disable_implicit_join_references = true`. 

 DEPRECATION WARNING: It looks like you are eager loading table(s) 
 (one of: product_masters, product_master_names) that are referenced 
 in a string SQL snippet. For example: 

Post.includes(:comments).where("comments.title = 'foo'")

这是导致错误的行

def find_product_master
    masters = ProductMaster.includes(:product_master_names).where("gcc = 1 and crossed_product_master_id is null and product_masters.supplier_id is null && (product_masters.slug = :name or product_masters.slug = :clean_name or product_master_names.slug = :name or product_master_names.slug = :clean_name) and product_masters.color = :color and (product_subfamily_id = :subfamily_id or second_product_subfamily_id = :subfamily_id)", name: line.file_name.parameterize, clean_name: line.file_name.parameterize.gsub("chateau","").parameterize, color: line.file_color, subfamily_id: line.product_subfamily_id)
    masters.size == 1 ? line.update_column(:product_master_id, masters.first.id) : line.update_column(:status, "product_master_missing")
end

我试过了,就像警告中描述的那样

 def find_product_master
    masters = ProductMaster.includes(:product_master_names).where("gcc = 1 and crossed_product_master_id is null and product_masters.supplier_id is null && (product_masters.slug = :name or product_masters.slug = :clean_name or product_master_names.slug = :name or product_master_names.slug = :clean_name) and product_masters.color = :color and (product_subfamily_id = :subfamily_id or second_product_subfamily_id = :subfamily_id)".references(:product_master_names), name: line.file_name.parameterize, clean_name: line.file_name.parameterize.gsub("chateau","").parameterize, color: line.file_color, subfamily_id: line.product_subfamily_id)
    masters.size == 1 ? line.update_column(:product_master_id, masters.first.id) : line.update_column(:status, "product_master_missing")
 end

然后我收到一个错误

 NoMethodError:
    undefined method `references' for #<String:0x00000010f2c730>

【问题讨论】:

  • 你能把这个东西缩小到一个更小的版本来重现你的问题吗?我想弄清楚发生了什么,因为我必须滚动才能看到任何东西。
  • 问题来自masters = ProductMaster.includes(:product_master_names).where("...") 行我的警告告诉:It looks like you are eager loading table(s) that are referenced in a string SQL snippet 警告还说我只需要.references(:my_table) 进行修复。但是在将其添加到该行之后,我收到了 undefined method references' 的 #<0x00000010f2c730>

标签: ruby ruby-on-rails-4 rspec


【解决方案1】:

只需在include 调用后面链接references,而不是在where 的参数之一上调用reference

masters = ProductMaster
  .includes(:product_master_names)
  .references(:product_master_names)
  .where(
    # ...
  )

【讨论】:

  • 我可以在 4 小时内进行测试,但这对我来说很奇怪。如果您查看警告,您会看到它给了我一个如何解决的示例,并且参考在字符串之后。无论如何,我会尝试,thx
  • 顺序不重要。您也可以在where 之后链接reference。但是您在 where 调用的参数上调用了 reference。这导致了错误undefined method 'references' for #&lt;String...
猜你喜欢
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多