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

Figure out how to use bind parameters for time queries #1285

Closed
bensheldon opened this issue Mar 15, 2024 · 1 comment
Closed

Figure out how to use bind parameters for time queries #1285

bensheldon opened this issue Mar 15, 2024 · 1 comment

Comments

@bensheldon
Copy link
Owner

bensheldon commented Mar 15, 2024

TIL, even when using Arel, that times still get serialized into the query rather than set up as bind parameters.

scope :only_scheduled, -> { where(arel_table['scheduled_at'].lteq(Time.current)).or(where(scheduled_at: nil)) }

I haven't figured out the perfect Rails 6.0+ and JRuby compatible recipe, but something like this should work I hope!

 scope :only_scheduled, -> { where(arel_table['scheduled_at'].lteq(Arel::Nodes::BindParam.new(ActiveModel::Attribute.with_cast_value("created_at", Time.current, ActiveModel::Type::Time)))).or(where(scheduled_at: nil)) } 

This is important because when the it's done as a bind parameter, it allows the prepared statement cache to work properly (otherwise it fills up with tons of slightly different queries)

@bensheldon
Copy link
Owner Author

Closed by: #1287 and #1308

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

No branches or pull requests

1 participant