【问题标题】:Rails SQLException syntax errors (Model.where('date > ? AND date < ?, min, max)Rails SQLException 语法错误(Model.where('date > ? AND date < ?, min, max)
【发布时间】:2014-01-14 01:40:16
【问题描述】:

我正在尝试查找两个日期时间之间的“事件”。

    if (params[:timeMin].present? and params[:timeMax].present?)
      @events = Event.where('date > ? AND date < ?', params[:timeMin], params[:timeMax])

我不断收到类似的错误,我不知道如何解决它。

SQLite3::SQLException: near ",": syntax error: SELECT "events".* FROM "events"  WHERE (date > '---
- (1i)
- ''2014''
','---
- (2i)
- ''1''
','---
- (3i)
- ''8''
','---
- (4i)
- ''14''
','---
- (5i)
- ''36''
' AND date < '---
- (1i)
- ''2014''
','---
- (2i)
- ''1''
','---
- (3i)
- ''13''
','---
- (4i)
- ''14''
','---
- (5i)
- ''36''
') ORDER BY events.created_at DESC

这是导致视图中问题的行

<% @events.each do |event| %>

谢谢

要求的信息

index.html.erb

<%= form_tag events_path, :method => :get do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= datetime_select :timeMin, params[:timeMin] %>
    <%= datetime_select :timeMax, params[:timeMax] %>
    <%= submit_tag "Search Near", :name => nil %>
  </p>
<% end %>

使用来自@CDub 的解决方案

在发出请求时记录应该返回 2 个(所有)事件的日期 -> 没有返回(空列表)

Started GET "/events?utf8=%E2%9C%93&search=&timeMin%5B%281i%29%5D=2014&timeMin%5B%282i%29%5D=1&timeMin%5B%283i%29%5D=7&timeMin%5B%284i%29%5D=15&timeMin%5B%285i%29%5D=13&timeMax%5B%281i%29%5D=2014&timeMax%5B%282i%29%5D=1&timeMax%5B%283i%29%5D=13&timeMax%5B%284i%29%5D=15&timeMax%5B%285i%29%5D=13" for 127.0.0.1 at 2014-01-13 18:13:45 -0800
Processing by EventsController#index as HTML
  Parameters: {"utf8"=>"✓", "search"=>"", "timeMin"=>{"(1i)"=>"2014", "(2i)"=>"1", "(3i)"=>"7", "(4i)"=>"15", "(5i)"=>"13"}, "timeMax"=>{"(1i)"=>"2014", "(2i)"=>"1", "(3i)"=>"13", "(4i)"=>"15", "(5i)"=>"13"}}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'BVjMjs3hJ1HV6udJzEg4-g' LIMIT 1
  Event Load (0.1ms)  SELECT "events".* FROM "events" WHERE (date > '2014-1-7 15:13:' AND date < '2014-1-13 15:13:') ORDER BY events.created_at DESC
  Rendered events/index.html.erb within layouts/application (9.1ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  Rendered layouts/_header.html.erb (1.1ms)
  Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 53ms (Views: 52.1ms | ActiveRecord: 0.3ms)

【问题讨论】:

    标签: sql ruby-on-rails-3 datetime


    【解决方案1】:

    您正在返回一个 datetime_select 哈希,需要将其解析为字符串。一种方法是执行以下操作:

    min_hash = params[:timeMin]
    max_hash = params[:timeMax]
    
    min_time_string = "#{min_hash['(1i)']}-#{min_hash['(2i)']}-#{min_hash['(3i)']} #{min_hash['(4i)']}:#{min_hash['(5i)']}:#{min_hash['(6i)']}"
    
    max_time_string = "#{max_hash['(1i)']}-#{max_hash['(2i)']}-#{max_hash['(3i)']} #{max_hash['(4i)']}:#{max_hash['(5i)']}:#{max_hash['(6i)']}"
    

    解析后,这应该可以工作:

    @events = Event.where('date > ? AND date < ?', min_time_string, max_time_string)
    

    【讨论】:

    • 添加了timeMin和timeMax来自的表格。希望这会有所帮助。试图找出rails中的模型搜索
    • 感谢您的帮助,但仍有问题。 1)如果我设置它应该选择事件的日期,它会运行并且不返回任何事件(列表为空)。 2) 如果我不更改任何一个日期(最小值 == 最大值),那么它会返回一个错误页面“Request-URI Too Large”。任何想法
    • 不确定我是否遵循问题 #1... 这两个问题的日志是什么样的?
    • 发布了日志。无论如何要查看每个模型的“日期”当前是什么
    • 当然。您可以将其粘贴在Rails.logger.info 调用中,它会打印到日志中。我也更新了我的答案——没有6i。我还使用Date.parse 实际生成了一个合法的Date 对象。试试看。
    猜你喜欢
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2023-03-12
    • 2019-06-25
    • 2022-12-28
    • 2022-07-16
    相关资源
    最近更新 更多