Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time values not being typecast for where and find queries on a date field #25354

Closed
mctaylorpants opened this issue Jun 10, 2016 · 1 comment
Closed

Comments

@mctaylorpants
Copy link
Contributor

I noticed this issue when I was getting inconsistent behaviour from passing find_or_create a Time object as the value for a query against a date field. create type-casts this value before saving, but it seems find and where do not.

The problem can obviously be fixed by calling #to_date on the Time object beforehand, or using a Date object in the first place, but I believe the type-casting behaviour should be consistent to avoid confusion.

Steps to reproduce

View Test Case

date_as_time = Time.new(2016,06,10,19,0,0,'-08:00')
Post.create(date: date_as_time)
# INSERT INTO "posts" ("date") VALUES (?)  [["date", Wed, 10 Jun 2016]]

Expected behavior

Post.where(date: date_as_time).count
# => 1

Post.find_by(date: date_as_time)
# => <Post id: 1, date: "2016-06-10">

Actual behavior

Post.where(date: date_as_time).count
# => 0

Post.find_by(date: date_as_time)
# => nil

System configuration

Rails version: 5.0.0.rc1

Ruby version: 2.3.0p0

kamipo added a commit to kamipo/rails that referenced this issue Jun 15, 2016
Currently `Type::Date#serialize` does not cast a value to a date object.
It should be cast to a date object for finding by date column correctly
working.

Fixes rails#25354.
@tmertens
Copy link

I am also observing this behavior on rails 4.2.5.2

kamipo added a commit to kamipo/rails that referenced this issue Jun 29, 2016
Currently `Type::Date#serialize` does not cast a value to a date object.
It should be cast to a date object for finding by date column correctly
working.

Fixes rails#25354.
MichaelSp pushed a commit to MichaelSp/rails that referenced this issue Nov 2, 2016
Currently `Type::Date#serialize` does not cast a value to a date object.
It should be cast to a date object for finding by date column correctly
working.

Fixes rails#25354.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants