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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:25:40+00:00 2026-06-03T09:25:40+00:00

I am new to Rail and trying to make a query in ActiveRecord. I

  • 0

I am new to Rail and trying to make a query in ActiveRecord. I am trying to get all of the records with the status of ‘Landed’, that are over 60 days old. My query works up to the point of getting all of the projects with the status of ‘Landed’. When I add in the last condition of “created_at < ? “, then I always get an empty relation. I know that I have projects that fit that description, so I am doing something wrong in my query and dont understand. I believe my error is in the date comparison, but I am not sure.

1. Projects
    belongs_to :status
    has_many :project_status_histories
2. Status
    has_many :projects
    has_many :project_status_histories
3. Project_Status_Histories
    belongs_to :status
    belongs_to :project

Project.find(:all, :joins => [:project_status_histories, :status], :conditions => {:projects => {:status_id => Status.where(:name => 'Landed').first.id }, :project_status_histories => {:created_at => ["created_at < ?", (Date.today - 60.days)]}})

I have tried to build the query, step by step, with the dbconsole and am not having any luck. Thanks for all the help in advance.

  • 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-06-03T09:25:41+00:00Added an answer on June 3, 2026 at 9:25 am

    I don’t think it’s the date arithmetic. One nice way to do this would be with named scopes. Add the following to project.rb:

    scope :landed, joins(:status).where('statuses.name' => 'Landed')
    scope :recent, lambda \
                  { joins(:project_status_histories) \
                  .where('project_status_histories.created_at < ?', Date.today - 60.days) } 
    

    Then you can retrieve the relevant records/objects with:

    Project.landed.recent
    

    This worked for me in my test. You should also check out the rails guide, from which I stole most of this:

    http://guides.rubyonrails.org/active_record_querying.html#scopes

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

Sidebar

Related Questions

new to c#. I'm trying to make a simple system where I can search
I love new Rail 3! The new query syntax is so awesome: users =
I'm new to Ruby on Rails, trying to get my first app working, which
I am trying to push a brand new, empty Rail 3.0.4 project to GitHub,
New to Node.js and Express, I am trying to understand the two seems overlapping
New programmer here, I am trying to understand and break down this code below
New to wordpress and web design in general. I'm trying to setup a website
New to Android so I'm writing small programs to get familiar with how things
I am trying to test to see if a simple run of a rail
new AjaxOptions { OnBegin = MyFunction, Url=/Controller/JSONAction/+OnbeginRetunrValue, HttpMethod=GET } In my Ajax properties, I

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.