好的,所以我在查询我的Job表时遇到了麻烦。我尝试做的是填写日历,其中包含与各自日期相对应的作业。在我的片段中,我使用了宝石撬。根据我当前的逻辑(控制台中的第28行),我的目标是搜索我的工作表,它将在同一天返回所有工作。现在当我渲染calendar时,每个日期都填充了所有5个Job对象。当我撬开你在控制台中看到的东西时,我得到第一个3.前3个都有相同的created_at日期,但我不知道为什么当我在控制台中只要求一个时,它会返回那些#39;案例。
供参考: @calender_days是一个填充Time对象格式的数组ex" 2018-05-16 08:30:00 -0400"
如果您需要更多信息并提前感谢您的帮助,请告诉我。
编辑:澄清。我不知道为什么当我使用时间对象搜索日期为16日时,我会在日期16日22日和16日获得3个对象。
21: <% 5.times do %>
22: <tr>
23: <% 7.times do %>
24: <td>
25: <% date = @calender_days[count] %>
=> 26: <% binding.pry %>
27: <u><%= date.strftime('%d') %></u><br />
28: <% @jobs.where(:datetime == date.day).each do |job| %>
29: <%= job.name %><br />
30: <% end %>
31: <% count += 1 %>
[1] pry(#<#<Class:0x6fc59a0>>)> @jobs.where(:datetime == Time.new("2018-05-16 08:30:00 -0400"))
Job Load (0.0ms) SELECT "jobs".* FROM "jobs"
=> [#<Job:0x74f20b0
id: 1,
name: "Sweep Floor",
datetime: Wed, 16 May 2018 08:30:00 EDT -04:00,
created_by: 1,
client_id: 9,
notes: "Get under couch",
workers: "3",
created_at: Thu, 17 May 2018 09:52:49 EDT -04:00,
updated_at: Thu, 17 May 2018 09:52:49 EDT -04:00>,
#<Job:0x74f1fd8
id: 2,
name: "Wash Windows",
datetime: Tue, 22 May 2018 11:30:00 EDT -04:00,
created_by: 2,
client_id: 10,
notes: "Remove all spots",
workers: "4",
created_at: Thu, 17 May 2018 09:52:49 EDT -04:00,
updated_at: Thu, 17 May 2018 09:52:49 EDT -04:00>,
#<Job:0x74f1ee8
id: 3,
name: "Clean Kitchen",
datetime: Wed, 16 May 2018 12:00:00 EDT -04:00,
created_by: 1,
client_id: 11,
notes: "Wipe down cabinets",
&#13;
这是我的所有Job对象。
[0] #<Job:0x65ef2a8> {
:id => 1,
:name => "Sweep Floor",
:datetime => Wed, 16 May 2018 08:30:00 EDT -04:00,
:created_by => 1,
:client_id => 9,
:notes => "Get under couch",
:workers => "3",
:created_at => Thu, 17 May 2018 09:52:49 EDT -04:00,
:updated_at => Thu, 17 May 2018 09:52:49 EDT -04:00
},
[1] #<Job:0x65ec638> {
:id => 2,
:name => "Wash Windows",
:datetime => Tue, 22 May 2018 11:30:00 EDT -04:00,
:created_by => 2,
:client_id => 10,
:notes => "Remove all spots",
:workers => "4",
:created_at => Thu, 17 May 2018 09:52:49 EDT -04:00,
:updated_at => Thu, 17 May 2018 09:52:49 EDT -04:00
},
[2] #<Job:0x65ec578> {
:id => 3,
:name => "Clean Kitchen",
:datetime => Wed, 16 May 2018 12:00:00 EDT -04:00,
:created_by => 1,
:client_id => 11,
:notes => "Wipe down cabinets",
:workers => "7",
:created_at => Thu, 17 May 2018 09:52:50 EDT -04:00,
:updated_at => Thu, 17 May 2018 09:52:50 EDT -04:00
},
[3] #<Job:0x65ec4b8> {
:id => 4,
:name => "Dusting",
:datetime => Wed, 16 May 2018 02:30:00 EDT -04:00,
:created_by => 1,
:client_id => 10,
:notes => "Get the fans.",
:workers => "8",
:created_at => Sat, 19 May 2018 08:49:11 EDT -04:00,
:updated_at => Sat, 19 May 2018 08:49:11 EDT -04:00
},
[4] #<Job:0x65ec3f8> {
:id => 5,
:name => "Dusting",
:datetime => Thu, 03 May 2018 08:30:00 EDT -04:00,
:created_by => 1,
:client_id => 11,
:notes => "TV Stand.",
:workers => "4",
:created_at => Sat, 19 May 2018 08:50:33 EDT -04:00,
:updated_at => Sat, 19 May 2018 08:50:33 EDT -04:00
}
&#13;
答案 0 :(得分:2)
我假设Jobs.datetime
列类型为datetime
,但您尝试按日期列出项目,而不是日期时间。我认为以下查询应该有效:
@jobs.where('DATE(datetime) = ?', Date.parse('Wed, 16 May 2018'))
DATE(...)
部分将datetime
转换为date
。