ruby on rails - How to build a table where each cell is built using the 2 dimensions -


desired output

each user has child plan has child plandate objects. plandate has attribute ddate date. plan has attribute mtype can either m, v, or c (haha mvc, subconscious techy much?). given week (let's current week), i'd print out table looks this:

---------------------------------------------------------------------------- |  user  |  mon  |  tue  |  wed  |  thu  |  fri  | other attributes of user ---------------------------------------------------------------------------- |  eric  |   m   |       |   m   |       |   m   |  ... ---------------------------------------------------------------------------- |  erin  |   v   |   v   |   v   |   v   |   v   |  ... ---------------------------------------------------------------------------- |  jace  |       |   c   |   c   |       |       |  ... ---------------------------------------------------------------------------- |  kris  |   c   |       |       |       |       |  ... ---------------------------------------------------------------------------- |  tina  |   v   |       |   v   |       |   v   |  ... ---------------------------------------------------------------------------- |  lily  |   m   |   m   |   m   |   m   |   m   |  ... ---------------------------------------------------------------------------- 

the order of users on rows doesn't matter me; may add ransack gem make ordered, ignore. given user may not have plandates ddate every day in given week, , there's no relationship between plandates across users.

proposed options

i feel there 2 options:

  1. in view, print column headers data-attribute of day in question, , print row headers data-attribute of user id in question (will have first select users have grandchild plandate ddate somewhere in current week). in intersection, use 2 data-attributes query activerecord.

  2. in model, generate data hash can create table, , pass hash view via controller.

option 1 makes more intuitive sense me having been lifelong excel user, breaks mvc entirely, i'm trying go option 2, , challenges below related option 2. said if can make case option 1 go it! feel if can convince me option 1, can implement without same problems...

challenges option 2

i can build hash 1 dimension key, , hash of other dimension array. example, if days of current week used key:

{   mon => [eric, erin, kris, tina, lily],   tue => [erin, jace, lily]   wed => [eric, erin, jace, kris, lily],   thu => [erin, lily],   fri => [eric, erin, tina, lily] } 

but problem once there, i'm not sure how deal fact there blanks in data... if convert hash above table, know how make users appear list under each date; wouldn't desired output @ all, because there wouldn't gaps in data. example on monday, there's no jace, there needs blank space jace it's easy viewer across , see, ah there's no jace on monday, there jace on tuesday , wednesday.

oh needed minute think logically this... need nested hash, 1 first dimension, 1 second. method this:

def plan_table     # 1 : array of current week's dates     week = (date.today.at_beginning_of_week..(date.today.at_end_of_week-2.days)).map { |d| d }     # 2 : find users (plucking id) have plan date in current week     current_week_users = user.select { |u| u.plans.select { |p| p.plan_dates.select { |pd| week.include? pd.ddate.to_date }.count > 0 }.count > 0 }.map(&:id)     # 3: build hash     @week_table = hash.new     week.each |day|         @week_table[day] = {}         current_week_users.each |user_id|             # each user in has built already, have check if user has pd that: 1) falls on date, 2) has non canceled plan, 3) has user matches current user. checks user_id , plan_id ole' paranoia objects being created without parents             potential_pd = plandate.select { |pd| pd.ddate.to_date == day && pd.plan_id != nil && pd.plan.status != "canceled" && pd.plan.user_id != nil && pd.plan.user.id == user_id }             if potential_pd == []                  @week_table[day][user_id] = ""             else                 @week_table[day][user_id] = potential_pd.first.plan.mtype             end         end     end     return @week_table end 

Comments