Created
September 9, 2011 12:40
-
-
Save josepjaume/1206106 to your computer and use it in GitHub Desktop.
Time comparison inconsistency with SQLite, PostgreSQL and MySQL with ActiveRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pending_notifications | |
notifications = self.notifications | |
if last_notification_read_at | |
# Sqlite doesn't properly compare dates because doesn't have a dedicated | |
# type for it. | |
# TODO: Investigate further and find a better solution for this. | |
# | |
time_restriction = if connection.class.name.demodulize =~ /SQLite/ | |
["datetime(notifications.created_at) >= ?", last_notification_read_at] | |
elsif connection.class.name.demodulize =~ /PostgreSQL/ | |
["notifications.created_at >= ?", last_notification_read_at.to_time] | |
else | |
["notifications.created_at >= ?", last_notification_read_at] | |
end | |
notifications = notifications.where(time_restriction) | |
end | |
notifications | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See this commit: rails/rails@20c07170
And this rails/rails#2892