简体   繁体   English

Rails 查询变为空,但如果在 sql 控制台中搜索,相同的查询会检索记录

[英]Rails query turns up empty, but same query retrieves records if serched in sql console

I've run into an issue after creating a scope to return all users with a birthday between two days along with some other conditions.在创建一个范围以返回所有用户的生日以及其他一些条件后,我遇到了一个问题。 This information is supposed to be presented to the user dashboard.该信息应该呈现给用户仪表板。

If I refresh the page, there are 0 records found.如果我刷新页面,找到 0 条记录。 If I run the scope in the Rails development environment console it also returns 0 records.如果我在 Rails 开发环境控制台中运行范围,它也会返回 0 条记录。 However, if I copy the query from the console when active logger is on, and put it straight into my SQL client, it returns the correct records.但是,如果我在活动记录器打开时从控制台复制查询,并将其直接放入我的 SQL 客户端,它会返回正确的记录。 This scope is chained with a few other scopes as well, but if I were to remove this scope from the query chain, the query works, it just doesn't filter the date of birth ranges like I want this scope to do.此范围也与其他一些范围链接在一起,但如果我要从查询链中删除此范围,则查询有效,它只是不会像我希望此范围那样过滤出生日期范围。

The SQL client is on the same database. SQL 客户端位于同一数据库上。

The scope:范围:

where(
  "(date_of_birth <= ?) OR DATE_ADD(date_of_birth, INTERVAL ? YEAR) BETWEEN ? AND ?", 
  min_age.years.ago.to_date, 
  min_age, 
  beginning_of_month, 
  end_of_month
)

The query:查询:

SELECT `drivers`.* FROM `drivers` WHERE ( (date_of_birth <=
'1990-02-05') OR DATE_ADD(date_of_birth, INTERVAL 23 YEAR) BETWEEN
'2013-04-01' AND '2013-04-30') AND (drivers.id NOT IN (SELECT
driver_id from follow_ups where team_id = 2)) AND
(average_miles_per_day >= 0.35) AND (travel_time < 2000) AND
(travel_time + (average_miles_per_day * 84) >= 2000)

Anytime Rails tries to execute this query it doesn't work.任何时候 Rails 尝试执行此查询都不起作用。 When executing this query in a SQL client, it works like a charm.在 SQL 客户端中执行此查询时,它就像一个魅力。

maybe... there is a problem with string to date conversion.也许...字符串到日期的转换有问题。 Try to convert in your model using STR_TO_DATE() function.尝试使用 STR_TO_DATE() 函数在您的模型中进行转换。 Please do it for each date... Let me know it it helps.请为每个日期做...让我知道它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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