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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:13:04+00:00 2026-05-27T08:13:04+00:00

Stuck on this one, if you could take a look.. :) What I want

  • 0

Stuck on this one, if you could take a look.. 🙂

What I want is to get all unfinished projects, including all unfinished tasks for a certain user.

This is my setup so far:

User (devise)
    has_one :employee

Employee
    belongs_to :user
    has_and_belongs_to_many :tasks
    has_and_belongs_to_many :unfinished_tasks, :conditions => { :tasks => { :completed_at => nil } }, :class_name => "Task"
    has_many :unfinished_projects, :through => :unfinished_tasks, :source => :project, :uniq => true   ( :include => :unfinished_tasks OR :include => :tasks ? )

Project
    has_many :tasks

Task
    belongs_to :project
    has_and_belongs_to_many :employees

In my view (haml) I’d like to have something like this:

- for project in current_user.employee.unfinished_projects

    = project.name

    # THESE ARE NOT THE ONLY THE TASKS FOR THE CURRENT_USER
    - for task in project.tasks    ( OR project.unfinished_tasks ? )

        = task.name

This setup works for the projects, there are only projects which have unfinished tasks.

But I’m not sure how to include the unfinished tasks with these projects.

Anyone knows the best way for doing this, I’d like to have a single query for all this if that’s possible.

EDIT:
The tricky part is that the tasks have to be for the current_user.
The projects are loaded perfectly.

But when it loads the tasks:

- for task in project.tasks.unfinished

It does this:

Task Load (1.3ms)  SELECT `tasks`.* FROM `tasks` WHERE `tasks`.`project_id` IN (12, 7, 13, 15, 14, 10, 16, 17, 9, 2, 3)
Task Load (0.4ms)  SELECT `tasks`.* FROM `tasks` WHERE `tasks`.`project_id` = 12 AND `tasks`.`completed_at` IS NULL
Task Load (0.3ms)  SELECT `tasks`.* FROM `tasks` WHERE `tasks`.`project_id` = 7 AND `tasks`.`completed_at` IS NULL
Task Load (0.6ms)  SELECT `tasks`.* FROM `tasks` WHERE `tasks`.`project_id` = 13 AND `tasks`.`completed_at` IS NULL
etc.

What it should do is get the tasks of the employee:

Employee
  Projects
    Tasks

Which should be the tasks that were inner joined in the projects query.

  • 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-27T08:13:04+00:00Added an answer on May 27, 2026 at 8:13 am

    So, after doing this all over again I didn’t really find what my problem was.
    There are a few things changed now: I’m using scopes, includes for eager load are in the scope, and no scope on the project.tasks itself.

    The project model:

    has_many :tasks
    scope :assigned_to, lambda { |employee| { :include => { :tasks => :employees }, :conditions => { :tasks => { :employees => { :id => employee.id } } } } }
    scope :unfinished, :include => :tasks, :conditions => { :tasks => { :completed_at => nil } }
    

    The view:

    - for project in Project.assigned_to( current_user.employee ).unfinished
      = project.name
      - for task in project.tasks
        = task.name
    

    Gives me this nice MySQL-query:

    SELECT `projects`.`id` AS t0_r0, `projects`.`name` AS t0_r1, `projects`.`description` AS t0_r2, `projects`.`completed_at` AS t0_r3, `projects`.`created_at` AS t0_r4, `projects`.`updated_at` AS t0_r5, `tasks`.`id` AS t1_r0, `tasks`.`name` AS t1_r1, `tasks`.`description` AS t1_r2, `tasks`.`estimated_duration` AS t1_r3, `tasks`.`calculated_duration` AS t1_r4, `tasks`.`status` AS t1_r5, `tasks`.`completed_at` AS t1_r6, `tasks`.`project_id` AS t1_r7, `tasks`.`created_at` AS t1_r8, `tasks`.`updated_at` AS t1_r9, `employees`.`id` AS t2_r0, `employees`.`alias` AS t2_r1, `employees`.`comments` AS t2_r2, `employees`.`user_id` AS t2_r3, `employees`.`created_at` AS t2_r4, `employees`.`updated_at` AS t2_r5 FROM `projects` LEFT OUTER JOIN `tasks` ON `tasks`.`project_id` = `projects`.`id` LEFT OUTER JOIN `employees_tasks` ON `employees_tasks`.`task_id` = `tasks`.`id` LEFT OUTER JOIN `employees` ON `employees`.`id` = `employees_tasks`.`employee_id` WHERE `employees`.`id` = 3 AND `tasks`.`completed_at` IS NULL
    

    Works like a charm!

    Special thanks to Taryn.. 😉

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

Sidebar

Related Questions

I am stuck on this one piece of code because I can't get the
I am designing a cookbook website and I am stuck on this one problem.
I'm stuck for ideas on this one. I'm working on a CMS that uses
This is probably a simple one to answer, but I'm stuck, so here goes.
I was trying Project Euler and am stuck on this one: A palindromic number
Please, take a look at my code bellow. This part pops top view controller
I know there are a lot of similar questions to this one around Stack
I defined a Window in WPF, into this one I put a stack panel
I'm playing with one stack overflow example. This example looks like this: void return_input
I've come a bit stuck this afternoon with a bug in my web application

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.