Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用rails上创建的字段比较时间范围_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何使用rails上创建的字段比较时间范围

Ruby on rails 如何使用rails上创建的字段比较时间范围,ruby-on-rails,Ruby On Rails,我需要在Rails字段中使用默认创建的_来获取仅包含在时间范围内的记录。这一天被创造出来了 我有这个: @start_time = test.start_time.strftime("%I:%M%p") @end_time test.end_time.strftime("%I:%M%p") @websites = device.websites.where(:created_at => @start_time..@end_time) 我在寻找这样的东西: @websites = devi

我需要在Rails字段中使用默认创建的_来获取仅包含在时间范围内的记录。这一天被创造出来了

我有这个:

@start_time = test.start_time.strftime("%I:%M%p")
@end_time test.end_time.strftime("%I:%M%p")
@websites = device.websites.where(:created_at => @start_time..@end_time)
我在寻找这样的东西:

@websites = device.websites.where(:created_at::time => @start_time..@end_time)
@websites = device.websites.where(:created_at => test.start_time..test.end_time)
这将生成此SQL语句,但没有结果

SELECT DISTINCT `websites`.* FROM `websites` WHERE `websites`.`device_id` = 2 AND (`websites`.`created_at` BETWEEN '16:10:00' AND '21:10:00')
但若我在字段中添加时间函数,效果会很好

SELECT DISTINCT `websites`.* FROM `websites` WHERE `websites`.`device_id` = 2 AND (time(`websites`.`created_at`) BETWEEN '16:10:00' AND '21:10:00')

Rails是如何做到这一点的?Rails会自动将时间对象转换为数据库所需的格式,因此不需要strftime。您应该能够这样做:

@websites = device.websites.where(:created_at::time => @start_time..@end_time)
@websites = device.websites.where(:created_at => test.start_time..test.end_time)

谢谢你的回答,可能是重复的!!我试过了,但不起作用,如果我使用mysql时间函数来转换在字段中创建的_,就可以了。