开发者

Finding Records by Date in Rails Console

开发者 https://www.devze.com 2023-03-30 06:46 出处:网络
This is happens on Sqlite. I am probably missing something but I couldn\'t figure out what exactly. Consol开发者_高级运维e Output

This is happens on Sqlite. I am probably missing something but I couldn't figure out what exactly.

Consol开发者_高级运维e Output

>> date = Task.first.date
=> Tue, 16 Aug 2011
>> Task.find(:all, :conditions => {:date => date})
=> []
>> Task.find(:all, :conditions => ['date = ?', date])
=> []
>> Task.find(:all, :conditions => ['date IS ?', date])
=> []

Schema

create_table "tasks", :force => true do |t|
  t.date     "date"
end


This is not a complete answer but hopefully will put your mind at ease that your syntax is correct

I knocked up a quick app to test this and all your methods of retrieval work. This was with an SQLite database also and there were no errors.

t = Task.create
 => #<Task id: 1, date: nil, created_at: "2011-08-23 14:32:27", updated_at: "2011-08-23 14:32:27"> 

t.date = "Tue, 16 Aug 2011"
 => "Tue, 16 Aug 2011" 

t.save

date = Task.last.date
 => Tue, 16 Aug 2011 

Task.find(:all, :conditions => {:date => date})
 => [#<Task id: 1, date: "2011-08-16">] 

Task.find(:all, :conditions => ['date = ?', date])
 => [#<Task id: 1, date: "2011-08-16">] 

Task.find(:all, :conditions => ['date IS ?', date])
 => [#<Task id: 1, date: "2011-08-16">] 

My schema.rb:

create_table "tasks", :force => true do |t|
  t.date "date"
end


Even I tried in my local db and everything seems to work fine.

The to_sql method will return the query generated by rails. That might help you in debugging the issue:

Task.where(:date => date).to_sql
0

精彩评论

暂无评论...
验证码 换一张
取 消