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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:09:09+00:00 2026-05-25T10:09:09+00:00

I’m building an app for assigning Shifts in a schedule, with or without an

  • 0

I’m building an app for assigning Shifts in a schedule, with or without an associated Employee. That is, a Shift with no Employee assigned, serves as a placeholder in the schedule.

On any given date, I want the schedule to show all Shifts on that date (consecutively ordered), and then show any remaining Employees, that are not scheduled to work on that date.

Here’s the models:

class Employee < ActiveRecord::Base
  belongs_to :account
  has_many :shifts
end

class Shift < ActiveRecord::Base
  belongs_to :account
  belongs_to :employee

  default_scope order(:starts_at)
end

and my problematic, hideous controller code (slightly edited for brevity):

class SchedulesController < ApplicationController
  include CurrentDateHelper

  def day
    @shifts = @account.shifts.includes(:employee).within(current_date_range).all
    @employees = @account.employees.alphabetically.all

    @employees.reject!{|employee| @shifts.map(&:employee).include? employee}
  end
end

and the (very simplified) view:

<tbody>
  <%- @shifts.each do |shift| -%>
    <%- if shift.employee.present? -%>
      <%- employee = shift.employee -%>
      <%- employee_shifts = shifts.select{|s| s.employee == employee} -%>

      <tr>
        <th><%= employee.name %></th>

        <%- employee_shifts.each do |shift| -%>
          <td><%= shift.timestamp %></td>
        <%- end -%>
      </tr>

    <%- else -%>
      <tr>
        <th>Unassigned</th>
        <td><%= shift.timestamp %></td>
      </tr>
    <%- end -%>
  <%- end -%>

  <%- @employees.each do |shift| -%>
    <tr>
      <th><%= employee.name %></th>
      <td>Click to create a shift for <%= employee.name %></td>
    </tr>
  <%- end -%>
</tbody>

My problem is that an Employee are currently displayed multiple times in the schedule, if/when that Employee have more than one Shift on the same date.
That, and the controller code is just ugly :S

I would probably need to group @shifts by Employee, and work from that…?
My SQL chops are not that good, so any pointers are greatly appreciated – I’m not necessarily looking for a copy/paste solution, more like some clues for me to dig into and learn from.

Thanks for reading through this – I hope it makes sense! 🙂

  • 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-25T10:09:09+00:00Added an answer on May 25, 2026 at 10:09 am

    You’re probably right in that you want to group by shifts, but you probably don’t want to do that with SQL. You do want to have access to that data in the view, and if you grouped by employee in the SQL, it would return only one shift per employee.

    Rather you probably want to use enumerable’s group_by, which in the view, would look something like:

    <% @shifts.group_by(&:employee).each do |employee, shifts| %>
       <%# do stuff for each 'employee' the grouped array of his/her 'shifts' %>
    <% end %>
    

    As for the other part. You could exclude the ids of the employees returned with the shifts. Look into Arel’s ‘not_in’ predicate to do that, e.g.

    ids = @shifts.map {|s| s.employee.try(:id) }.compact
    condition = Employee.arel_table[:id].not_in(ids)
    @employees = @account.employees.alphabetically.where(condition)
    
    # of if you extracted this into a scope 'exluding' on Employee, it could look like:
    @employees = @account.employees.alphabetically.excluding(@shifts.map &:employee)
    

    Or if you just want to replace the reject! business you have going on, you could simply subtract the shift-holding users from the array. e.g.:

    @employees = @account.employees.alphabetically.all - @shifts.map(&:employee)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT

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.