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 7615739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:43:00+00:00 2026-05-31T02:43:00+00:00

I have an ActiveRecord model that has a date attribute. Is it possible to

  • 0

I have an ActiveRecord model that has a date attribute. Is it possible to utilize that date attribute to find by Year, Day and Month:

Model.find_by_year(2012)
Model.find_by_month(12)
Model.find_by_day(1)

or is it simply possible to find_by_date(2012-12-1).

I was hoping I could avoid creating Year, Month and Day attributes.

  • 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-31T02:43:01+00:00Added an answer on May 31, 2026 at 2:43 am

    Assuming that your “date attribute” is a date (rather than a full timestamp) then a simple where will give you your “find by date”:

    Model.where(:date_column => date)
    

    You don’t want find_by_date_column as that will give at most one result.

    For the year, month, and day queries you’d want to use the extract SQL function:

    Model.where('extract(year  from date_column) = ?', desired_year)
    Model.where('extract(month from date_column) = ?', desired_month)
    Model.where('extract(day   from date_column) = ?', desired_day_of_month)
    

    However, if you’re using SQLite, you’d have to mess around with strftime since it doesn’t know what extract is:

    Model.where("cast(strftime('%Y', date_column) as int) = ?", desired_year)
    Model.where("cast(strftime('%m', date_column) as int) = ?", desired_month)
    Model.where("cast(strftime('%d', date_column) as int) = ?", desired_day_of_month)
    

    The %m and %d format specifiers will add leading zeroes in some case and that can confuse the equality tests, hence the cast(... as int) to force the formatted strings to numbers.

    ActiveRecord won’t protect you from all the differences between databases so as soon as you do anything non-trivial, you either have to build your own portability layer (ugly but sometimes necessary), tie yourself to a limited set of databases (realistic unless you’re releasing something that has to run on any database), or do all your logic in Ruby (insane for any non-trivial amount of data).

    The year, month, and day-of-month queries will be pretty slow on large tables. Some databases let you add indexes on function results but ActiveRecord is too stupid to understand them so it will make a big mess if you try to use them; so, if you find that these queries are too slow then you’ll have to add the three extra columns that you’re trying to avoid.

    If you’re going to be using these queries a lot then you could add scopes for them but the recommended way to add a scope with an argument is just to add a class method:

    Using a class method is the preferred way to accept arguments for scopes. These methods will still be accessible on the association objects…

    So you’d have class methods that look like this:

    def self.by_year(year)
        where('extract(year from date_column) = ?', year)
    end
    # etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model class that has, among other things: class Group < ActiveRecord::Base
Let's say I have an ActiveRecord model called Book that has a has_many association
I have an ActiveRecord model that has two database attributes, total and processing_fees .
I have an ActiveRecord model that has a long string field. Actually, this string
I have an ActiveRecord Model, PricePackage. That has a before_create call back. This call
I have an ActiveRecord model that I would like to convert to xml, but
I have a basic ActiveRecord model in which i have two fields that i
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have an ActiveRecord model, Foo , which has a name field. I'd like
I have an ActiveRecord model class Foo that has_many Bar . I want to

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.