简体   繁体   中英

Datetime format in Ruby doesn't work for identifier

I have a question about the format of t.datetime.

When I command in the rails console,

Lesson.find(1781).start_time, 

then

the Tue, 03 May 2016 14:00:00 UTC +00:00

is returned.

If I command

Classtime.find_by(start_time: 'Tue, 03 May 2016 14:00:00 UTC +00:00')

, it returns nil.

But if I command

Classtime.find_by(start_time: Lesson.find(1781).start_time) 

, it returns

<Classtime id: 4429, start_time: "2016-05-03 14:00:00", created_at: "2016-04-21 15:53:22", updated_at: "2016-04-21 15:53:22"> 

So I guess, Lesson.find(1781).start_time is not equal to the return of Lesson.find(1781).start_time in some sense. How would I be able to know what caused this?

Please share with me!!!

This:

Classtime.find_by(start_time: Lesson.find(1781).start_time)

is the same as:

Classtime.find_by(start_time: '2016-05-03 14:00:00')

which is NOT the same as:

Classtime.find_by(start_time: 'Tue, 03 May 2016 14:00:00 UTC +00:00')

The database knows how to translate the string 2016-05-03 14:00:00 to a date, but not the string Tue, 03 May 2016 14:00:00 UTC +00:00 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM