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

  • Home
  • SEARCH
  • 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 6187349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:02:36+00:00 2026-05-24T02:02:36+00:00

Is it possible to do an activerecord query that only looks at the time

  • 0

Is it possible to do an activerecord query that only looks at the time component of a datetime field?

e.g. Battle.where('start_time < ? and start_time > ?','12:00','06:00')

to find all battles that were started between 6am and 12pm regardless of the day they occurred? In this example, start_time is defined as a datetime.

  • 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-24T02:02:38+00:00Added an answer on May 24, 2026 at 2:02 am

    The only way to do this is using a SQL function, if you’re on MySQL you could do it like this:

    Battle.where( 'HOUR( start_time ) >= ? AND HOUR( start_time ) <= ?', 12, 6 )
    

    But this is hugely inefficient and is always going to generate a full table scan and you surely don’t want that.

    The best solution is to add columns with the hour values alone at your battle model, index them and query directly on them like this:

    Battle.where( 'start_time_hour >= ? start_time_hour <= ?', 12, 6 )
    

    Just add an before_save callback that sets this values before saving your Battle model based on the start_time property.

    EDIT

    BTW, if you’re using PostgreSQL, it’s capable of creating an index on the result of a function, so, if you don’t mind having to stick with PostgreSQL you could just use the SQL function. And before you ask, no, i don’t know how it works, I only know the database allows you to do so and that this is not available on MySQL.

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

Sidebar

Related Questions

Is it possible to query unsaved changes using Rail's ActiveRecord or another similar approach?
Possible Duplicate: Model.find(1) gives ActiveRecord error when id 1 does not exist If there
I'm building an abstract gem. i need a sql query that looks like this
Is it possible to add a callback to a single ActiveRecord instance? As a
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
It seems when I grab some hierarchical ActiveRecord structure that there are quite a
I have an active record Song model with a songhash field (string(255)) that contains
Is it possible to map an ActiveRecord class using the MetadataType attribute in order
In my Rails app, I have the following PostgreSQL query to find the most
Is it possible to configure the default settings on Castle Activerecord attributes? For example,

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.