ruby-on-rails – DEPRECATION警告:使用非属性参数调用的危险查
发布时间:2020-12-17 03:25:25 所属栏目:百科 来源:网络整理
导读:我将rails 5.1.4 app更新为5.2.0.我的一个模型中有以下范围: scope :by_category,lambda { |category_slug| category_ids = Category.find_by(slug: category_slug).subtree_ids where(category_id: category_ids) } 由于该范围,Rails返回错误: DEPRECATIO
我将rails 5.1.4 app更新为5.2.0.我的一个模型中有以下范围:
scope :by_category,lambda { |category_slug| category_ids = Category.find_by(slug: category_slug)&.subtree_ids where(category_id: category_ids) } 由于该范围,Rails返回错误: DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "coalesce("categories"."ancestry",'')". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values,such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql() 我该如何解决这个问题? 解决方法
问题是
ordered_by_ancestry scope:
scope :ordered_by_ancestry,Proc.new { |order| if %w(mysql mysql2 sqlite sqlite3 postgresql).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::MAJOR >= 5 reorder("coalesce(#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)},'')",order) else reorder("(CASE WHEN #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)} IS NULL THEN 0 ELSE 1 END),#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}",order) end } 正在将一个原始的SQL字符串传递给#reorder,并且作为警告状态,这在Rails 5.2中已被弃用(并且将在Rails 6中完全删除). 刚提交了一个pull request,通过在Arel.sql调用中包装这些字符串来解决这个问题.我希望这会快速合并(尽管拉请求在第二个分支中缺少Arel.sql调用)但同时,你有一些选择: >忽略警告并等待修补宝石. def self.ordered_by_ancestry(order) reorder(Arel.sql("coalesce(#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)},'')"),order) end 并等待拉取请求合并. 更新:解决此警告的拉取请求是just merged,因此无需再等待,您应该能够从GitHub获取最新信息并继续进行更多有趣的事情.感谢kbrock对此进行整理. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |