简体   繁体   中英

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. If I run the scope in the Rails development environment console it also returns 0 records. 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. 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.

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. When executing this query in a SQL client, it works like a charm.

maybe... there is a problem with string to date conversion. Try to convert in your model using STR_TO_DATE() function. Please do it for each date... Let me know it it helps.

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