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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:32:41+00:00 2026-06-08T00:32:41+00:00

I have a time tracking application. An Hour belongs_to a Project . And a

  • 0

I have a time tracking application.

An Hour belongs_to a Project. And a Project belongs to a ProjectStatus and thus has a field project_status_id.

Now I want to scope my Hour model to filter all hours that belong to projects with a certain status, e.g. 2.

In my Project model I have this scope:

  #project.rb
  scope :continous, where(:project_status_id => '2')

In my Hour model I tried the following scope but it always returns an empty array, also I know there are hours that math my criteria:

  #hour.rb
  scope :intern, joins(:project) & Project.continous

also I think the generated SQL output on the console looks a little strange:

1.9.2-p180 :003 > Hour.intern
  Hour Load (104.5ms)  SELECT `hours`.* FROM `hours` INNER JOIN `projects` ON `projects`.`id` = `hours`.`project_id`
  Project Load (6.1ms)  SELECT `projects`.* FROM `projects` WHERE (project_status_id <> 2)
  Hour Load (95.9ms)  SELECT `hours`.* FROM `hours` INNER JOIN `projects` ON `projects`.`id` = `hours`.`project_id`
  Project Load (1.0ms)  SELECT `projects`.* FROM `projects` WHERE `projects`.`project_status_id` = 2
  Hour Load (92.6ms)  SELECT `hours`.* FROM `hours` 
 => [] 

Why is there one query with WHERE (project_status_id <> 2) and one with WHERE 'projects'.'project_status_id' = 2 ?

Is he trying to subtract the two from each other ?
Any hints how to make this scope work.. ?
thanks

  • 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-08T00:32:43+00:00Added an answer on June 8, 2026 at 12:32 am

    Not sure if this is the most elegant approach, but you can merge relation objects:

    scope :intern, lambda { joins(:project).merge(Project.continous) }
    

    Note that I’m wrapping it in a lambda so Project.continuous is not executed when the application loads. Also, this is the preferred way to define scopes in future versions of Rails.

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

Sidebar

Related Questions

I do not want Auditing or History tracking. I have an application that pulls
I have a task tracking application, which is very basic. It currently has a
I have time in NSString like 15:15 I want to covert this NSString time
In My application have time consuming process.There fore i try to do that operation
Right now I don't have time to optimize and to upgrade servers and once
I'm working on an application in C# with .Net 3.5. I have time zone
I am making an object tracking application. I have used Emgucv 2.1.0.0 to load
I am looking for an issue tracking application, which has two levels in its
I am doing a project called user initiated real time object tracking system .
I have been tasked with designing an application for tracking inventory on a mobile

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.