Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7648851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:49:14+00:00 2026-05-31T10:49:14+00:00

This has been bugging me for a while… How can I string interpolate a

  • 0

This has been bugging me for a while…

How can I string interpolate a datetime in Rails ActiveRecord queries?

# Works, but supeh ugleh:
Model.where("created_at >= ?", Time.now - 5.days)

# How do I do this?
Model.where("created_at >= #{Time.now - 5.days}")
# As is, it produces the following error message:
# ActiveRecord::StatementInvalid: PG::Error: ERROR:  syntax error at or near ...

The reason I care is for code readability:

# I like this better:
Model.where("created_at >= #{Time.now - 5.days} OR" + \
            "updated_at >= #{Time.now - 3.days}")

# than this:
Model.where("created_at >= ? OR updated_at >= ?", Time.now - 5.days, Time.now - 3.days)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:49:15+00:00Added an answer on May 31, 2026 at 10:49 am

    I’d advise against using string interpolation for this, there are many sharp edges and you’ll probably have more fun bobbing for apples in a bucket of fish hooks. You should do it this way:

    Model.where(
        'created_at >= :five_days_ago or updated_at >= :three_days_ago',
        :five_days_ago  => Time.now - 5.days,
        :three_days_ago => Time.now - 3.days
    )
    

    Using (well) named placeholders gives you the readability and position independence that you think string interpolation offers but nicely sidesteps the quoting, timezone, and format issues that string interpolation forces on you.

    But how do you safely use string interpolation? There are a few things you must handle yourself:

    1. Quoting and escaping.
    2. Timestamp formats.
    3. Maybe timezones too.

    ActiveRecord will take care of all this nonsense for you.

    Don’t try to do the quoting yourself, use the driver’s quoting methods. You will have access to connection.quote for properly quoting strings.

    Any database will know what to do with ISO 8601 timestamps and there is a convenient iso8601 method for that. ISO 8601 also conveniently includes the timezone and the database should be able to parse that (but if it can’t then you’ll have to convert your times to UTC by hand with .utc).

    So, to be safe:

    Model.where("created_at >= #{connection.quote((Time.now - 5.days).utc.iso8601)} " + \
             "OR updated_at >= #{connection.quote((Time.now - 3.days).utc.iso8601)}")
    

    Not so pretty now is it? With ISO 8601 timestamps you should be safe replacing the connection.quote calls with simple single quotes:

    Model.where("created_at >= '#{(Time.now - 5.days).utc.iso8601}' " + \
             "OR updated_at >= '#{(Time.now - 3.days).utc.iso8601}'")
    

    but you still have a lot of noise and ugliness and you’ll be developing bad habits.

    We’re not partying like PHP programmers in 1999 so don’t give in to false laziness by using string interpolation in your SQL, use named placeholders.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has been bugging me for a while now, but I never seem
This has been bugging me for a while but when I databind a control
This one has been bugging me for a while now. Is there a way
This Jquery problem has been bugging me for a while now. I developed a
This is a question that has been bugging me for a while. I started
It's not a programming questions. But this question has been bugging me for awhile.
This question has been bugging me for a while. I am looking for a
This has been bugging me for a while, so I asked a coworker if
This has been bugging me for a while. How do GCC and g++ compile
This problem has been bugging me for a while. I have to load a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.