在 rails 中的方法之间使用 arel 和 gt lt 获取日期查询
Get dates Queries with arel and gt lt between methods in rails
我想获取比某个特定日期大的日期,或者比那个日期小的日期,甚至 介于[之间的日期] =35=] 两个给定日期 arel。这是我的控制器:
@from_time = search[:value][:from_time]
@to_time = search[:value][:to_time]
letters_query = letters[@key].between(@from_time..@to_time)
@letters = Letter.joins(:official).where(letters_query)
这是我传递给控制器的 json
Parameters: {"query"=>{"search"=>{"0"=>{"key"=>"letter_date", "value"=>{"from_time"=>"01/09/1395", "to_time"=>"30/09/1395"}, "type"=>"date"}}}}
我确定要获取参数,请注意我的日期是太阳类型的!
它returns这个错误:
If you are passing user input to a predicate, you must either give an appropriate type caster object to the `Arel::Table`, or manually cast the value before passing it to Arel.
DEPRECATION WARNING: Passing a column to `quote` has been deprecated. It is only used for type casting, which should be handled elsewhere. See https://github.com/rails/arel/commit/6160bfbda1d1781c3b08a33ec4955f170e95be11 for more information. (called from p at /home/afsane/Desktop/Afsane Development/Production/isecretariat/app/controllers/searches_controller.rb:511)
DEPRECATION WARNING: table_exists? is deprecated and will be removed from Rails 5.1 (use #data_source_exists? instead) (called from p at /home/afsane/Desktop/Afsane Development/Production/isecretariat/app/controllers/searches_controller.rb:511)
Arel performing automatic type casting is deprecated, and will be removed in Arel 8.0. If you are seeing this, it is because you are manually passing a value to an Arel predicate, and the `Arel::Table` object was constructed manually. The easiest way to remove this warning is to use an `Arel::Table` object returned from calling `arel_table` on an ActiveRecord::Base subclass.
If you're certain the value is already of the right type, change `attribute.eq(value)` to `attribute.eq(Arel::Nodes::Quoted.new(value))` (you will be able to remove that in Arel 8.0, it is only required to silence this deprecation warning).
You can also silence this warning globally by setting `$arel_silence_type_casting_deprecation` to `true`. (Do NOT do this if you are a library author)
If you are passing user input to a predicate, you must either give an appropriate type caster object to the `Arel::Table`, or manually cast the value before passing it to Arel.
对于大的和小的我用这个:
letters_query = officials[@key].lt(@to_time)
letters_query = officials[@key].gt(@from_time)
我测试了很多,所有问题都出在这个 line:
@letters = Letter.joins(:official).where(letters_query)
有什么想法吗?
我认为您可以在此处使用 parsi-date 作为 gem 中的年份,您的问题似乎与我相似。您将需要相应地解析您的查询参数。如果您的数据库中存储的日期是基督教日历格式,您将执行 to_gregorian
。
@from_time = Parsi::Date.parse(search[:value][:from_time]).to_gregorian
@to_time = Parsi::Date.parse(search[:value][:to_time]).to_gregorian
使用 arel,这是为日期生成合适查询的方法:
letters = Letter.arel_table
@letter = Letter.where(letters[:date].between(Date.new(2016,08,24)..Date.new(2016,09,24))
我想获取比某个特定日期大的日期,或者比那个日期小的日期,甚至 介于[之间的日期] =35=] 两个给定日期 arel。这是我的控制器:
@from_time = search[:value][:from_time]
@to_time = search[:value][:to_time]
letters_query = letters[@key].between(@from_time..@to_time)
@letters = Letter.joins(:official).where(letters_query)
这是我传递给控制器的 json
Parameters: {"query"=>{"search"=>{"0"=>{"key"=>"letter_date", "value"=>{"from_time"=>"01/09/1395", "to_time"=>"30/09/1395"}, "type"=>"date"}}}}
我确定要获取参数,请注意我的日期是太阳类型的! 它returns这个错误:
If you are passing user input to a predicate, you must either give an appropriate type caster object to the `Arel::Table`, or manually cast the value before passing it to Arel.
DEPRECATION WARNING: Passing a column to `quote` has been deprecated. It is only used for type casting, which should be handled elsewhere. See https://github.com/rails/arel/commit/6160bfbda1d1781c3b08a33ec4955f170e95be11 for more information. (called from p at /home/afsane/Desktop/Afsane Development/Production/isecretariat/app/controllers/searches_controller.rb:511)
DEPRECATION WARNING: table_exists? is deprecated and will be removed from Rails 5.1 (use #data_source_exists? instead) (called from p at /home/afsane/Desktop/Afsane Development/Production/isecretariat/app/controllers/searches_controller.rb:511)
Arel performing automatic type casting is deprecated, and will be removed in Arel 8.0. If you are seeing this, it is because you are manually passing a value to an Arel predicate, and the `Arel::Table` object was constructed manually. The easiest way to remove this warning is to use an `Arel::Table` object returned from calling `arel_table` on an ActiveRecord::Base subclass.
If you're certain the value is already of the right type, change `attribute.eq(value)` to `attribute.eq(Arel::Nodes::Quoted.new(value))` (you will be able to remove that in Arel 8.0, it is only required to silence this deprecation warning).
You can also silence this warning globally by setting `$arel_silence_type_casting_deprecation` to `true`. (Do NOT do this if you are a library author)
If you are passing user input to a predicate, you must either give an appropriate type caster object to the `Arel::Table`, or manually cast the value before passing it to Arel.
对于大的和小的我用这个:
letters_query = officials[@key].lt(@to_time)
letters_query = officials[@key].gt(@from_time)
我测试了很多,所有问题都出在这个 line:
@letters = Letter.joins(:official).where(letters_query)
有什么想法吗?
我认为您可以在此处使用 parsi-date 作为 gem 中的年份,您的问题似乎与我相似。您将需要相应地解析您的查询参数。如果您的数据库中存储的日期是基督教日历格式,您将执行 to_gregorian
。
@from_time = Parsi::Date.parse(search[:value][:from_time]).to_gregorian
@to_time = Parsi::Date.parse(search[:value][:to_time]).to_gregorian
使用 arel,这是为日期生成合适查询的方法:
letters = Letter.arel_table
@letter = Letter.where(letters[:date].between(Date.new(2016,08,24)..Date.new(2016,09,24))