Skip to content

Instantly share code, notes, and snippets.

@josepjaume
Created September 9, 2011 12:40
Show Gist options
  • Save josepjaume/1206106 to your computer and use it in GitHub Desktop.
Save josepjaume/1206106 to your computer and use it in GitHub Desktop.
Time comparison inconsistency with SQLite, PostgreSQL and MySQL with ActiveRecord
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
@fesplugas
Copy link

See this commit: rails/rails@20c07170
And this rails/rails#2892

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment