Rails 7.2 用符号打破 find_by

问题描述 投票:0回答:1

我正在将 Rails 应用程序从

7.1.3.2
升级到
7.2.1,
,但我遇到了
find_by.

的问题

在我的控制器中,我有

ControlPlan.find_by("code = :code AND validity_dates @> date :date",
                    code: hash_params[:control_plan_code],
                    date: hash_params[:inspection_started_on])

7.1.3.2
中运行良好,但在升级后会生成错误。该问题与
validity_dates
的插值有关。

如下控制台所示,如果我使用字符串作为日期,则查询可以工作,但如果我使用符号,即使生成的 SQL 看起来相同,也会收到错误。

s(dev)> ControlPlan.where("validity_dates @> date '2020-12-24'").to_sql
=> "SELECT \"control_plans\".* FROM \"control_plans\" WHERE (validity_dates @> date '2020-12-24')"
s(dev)> ControlPlan.where("validity_dates @> date '2020-12-24'")
  ControlPlan Load (4.3ms)  SELECT "control_plans".* FROM "control_plans" WHERE (validity_dates @> date '2020-12-24') /* loading for pp */ LIMIT $1  [["LIMIT", 11]]
=> []
s(dev)> ControlPlan.where("validity_dates @> date :date", date: "2020-12-24").to_sql
=> "SELECT \"control_plans\".* FROM \"control_plans\" WHERE (validity_dates @> date '2020-12-24')"
s(dev)> ControlPlan.where("validity_dates @> date :date", date: "2020-12-24")
An error occurred when inspecting the object: #<ActiveRecord::StatementInvalid:"PG::SyntaxError: ERROR:  syntax error at or near \"$1\"\nLINE 1: ...ROM \"control_plans\" WHERE (validity_dates @> date $1) /* loa...\n                                                             ^\n">
Result of Kernel#inspect: #<ControlPlan::ActiveRecord_Relation:0x000000010c1685c0 @klass=ControlPlan(id: integer, created_at: datetime, updated_at: datetime, validity_dates: daterange, code: integer, state: enum, edition: integer), @table=#<Arel::Table:0x000000010d883340 @name="control_plans", @klass=ControlPlan(id: integer, created_at: datetime, updated_at: datetime, validity_dates: daterange, code: integer, state: enum, edition: integer), @type_caster=#<ActiveRecord::TypeCaster::Map:0x000000010daa7bf8 @klass=ControlPlan(id: integer, created_at: datetime, updated_at: datetime, validity_dates: daterange, code: integer, state: enum, edition: integer)>, @table_alias=nil>, @values={:where=>#<ActiveRecord::Relation::WhereClause:0x000000010cfabf38 @predicates=[#<Arel::Nodes::BoundSqlLiteral "(validity_dates @> date :date)" {:date=>"2020-12-24"}>]>}, @loaded=nil, @predicate_builder=#<ActiveRecord::PredicateBuilder:0x000000010da85da0 @table=#<ActiveRecord::TableMetadata:0x000000010da85e90 @klass=ControlPlan(id: integer, created_at: datetime, updated_at: datetime, validity_dates: daterange, code: integer, state: enum, edition: integer), @arel_table=#<Arel::Table:0x000000010d883340 @name="control_plans", @klass=ControlPlan(id: integer, created_at: datetime, updated_at: datetime, validity_dates: daterange, code: integer, state: enum, edition: integer), @type_caster=#<ActiveRecord::TypeCaster::Map:0x000000010daa7bf8 @klass=ControlPlan(id: integer, created_at: datetime, updated_at: datetime, validity_dates: daterange, code: integer, state: enum, edition: integer)>, @table_alias=nil>, @reflection=nil>, @handlers=[[Set, #<ActiveRecord::PredicateBuilder::ArrayHandler:0x000000010da85328 @predicate_builder=#<ActiveRecord::PredicateBuilder:0x000000010da85da0 ...>>], [Array, #<ActiveRecord::PredicateBuilder::ArrayHandler:0x000000010da854b8 @predicate_builder=#<ActiveRecord::PredicateBuilder:0x000000010da85da0 ...>>], [ActiveRecord::Relation, #<ActiveRecord::PredicateBuilder::RelationHandler:0x000000010da85738>], [Range, #<ActiveRecord::PredicateBuilder::RangeHandler:0x000000010da85828 @predicate_builder=#<ActiveRecord::PredicateBuilder:0x000000010da85da0 ...>>], [BasicObject, #<ActiveRecord::PredicateBuilder::BasicObjectHandler:0x000000010da85cb0 @predicate_builder=#<ActiveRecord::PredicateBuilder:0x000000010da85da0 ...>>]]>, @delegate_to_klass=false, @future_result=nil, @records=nil, @async=false, @none=false, @should_eager_load=nil, @arel=nil, @to_sql=nil, @take=nil, @offsets=nil, @cache_keys=nil, @cache_versions=nil>

我检查了Rails 7.2发行说明,但我没有发现任何相关内容。

我可以按如下方式更新我的代码,但我宁愿保持现在的样子。

ControlPlan.find_by("code = :code AND validity_dates @> date '#{hash_params[:inspection_started_on]}'",
                    code: hash_params[:control_plan_code])

我在这里缺少什么?

ruby-on-rails activerecord
1个回答
0
投票
ControlPlan.find_by("code = :code AND validity_dates @> date :date",
                    code: hash_params[:control_plan_code],
                    date: Arel::Nodes.build_quoted(hash_params[:inspection_started_on]))
© www.soinside.com 2019 - 2024. All rights reserved.