rails-sqlserver/activerecord-sqlserver-adapter

Issue with date values in queries

aidanharan opened this issue · 2 comments

I am not sure if this is related, but we are still facing the same issue in Rails-7.2.1 and activerecord-sqlserver-adapter 7.2.1.
.where('date_column < ?', date) is throwing "undefined method `value_for_database'"

Originally posted by @aaronsajo in #1166 (comment)

I also came across this today.

where("written_on < ?", Time.now) ✅
where("written_on < ?", Date.today) ❌
where("written_on < ?", DateTime.current) ❌
So, it seems to work only for Time objects, but not Date or DateTime objects.

Adding checks for type.is_a?(Date) || type.is_a?(DateTime) in #basic_attribute_type? would fix these specific cases, but I don't know the codebase well enough to say, if this would break something else.

I have something that may or may not be related to this. I have the same problems with dates. When I do this:

MyModel.where(updated_at: (10.days.ago.to_date...Date.tomorrow)).to_sql

I get this answer:

SELECT [my_models].* FROM [my_models] WHERE [my_models].[updated_at] >= '10-05-2024' AND [my_models].[updated_at] < '10-16-2024'"

However, I do not use american date formats. And I don't believe I am setting that anywhere. So maybe the problems is that dates do not default to iso-format as they used to?

andsip commented

@nielsjansendk: I think, if you don't set the format explicitly anywhere in your codebase, the format is fetched from the SQL Server.
Maybe you can run DBCC USEROPTIONS on your SQL Server and post the value of option dateformat here?

Or, if you are using Azure, the dateformat is fetched via SELECT [dateformat] FROM [sys].[syslanguages] WHERE [langid] = @@LANGID


So maybe the problems is that dates do not default to iso-format as they used to?

Was this the case, that default was iso format? I don't see this anywhere in the last few versions of the adapter.