【发布时间】: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 methodreferences' 的 #<0x00000010f2c730>0x00000010f2c730>
标签: ruby ruby-on-rails-4 rspec