0

Hi I am trying to build a rails meetings application. I have aa table called Meetings with 4 columns name:string start_date:datime end_date:datetime duration:integer

A meeting will have multible inputs per day for example a person can have two or three different meetings lasting different times throughout the day see image below

enter image description here

I want to be able to show one date per mutiple meetings and it tried using pluck but i get output = ["2021-08-01", "2021-08-02", "2021-08-06", "2021-08-07", "2021-08-10", "2021-08-05", "2021-08-28", "2021-08-29"]

this code works outside the table but not the way i would hope

<%= Meeting.distinct.pluck('date(start_date)')  %> 

How do i get it working within my meeting.index.erb table view ?

<h1>Meetings</h1>
<table class="table">
  <thead class="thead-dark">
    <tr>
      <th>Name</th>
      <th>Start date</th>
      <th>End date</th>
      <th>Duration</th>
      <th>Duration Time</th>
    </tr>
  </thead>

  <tbody>
    <% @meetings.each do |meeting| %>
      <tr>
        <td><%= meeting.name %></td>
        <td><%= meeting.start_date.strftime("%d-%b-%Y %I:%M %p") %></td>
        <td><%= meeting.end_date.strftime("%d-%b-%Y %I:%M %p") %></td>
        <td><%= meeting.duration %></td>
        <td><%= duration_display meeting  %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br><br> 

Maybe i am going about it the wrong if someone could point me in the best direction i would appreciate it

Sarah Duffy
  • 191
  • 3
  • 14

1 Answers1

2

There are probably many ways to do this. One idea is to pluck out the individual dates by day, then pull in the data that matches that date range using a range condition. https://guides.rubyonrails.org/active_record_querying.html#range-conditions

<h1>Meetings</h1>

<table class="table">
  <thead class="thead-dark">
    <tr>
      <th>Name</th>
      <th>Start date</th>
     <th>End date</th>
      <th>Duration</th>
      <th>Duration Time</th>
    </tr>
  </thead>

  <tbody>
  <% @meetings.pluck("start_date").map{ |x| x.to_date}.sort.uniq.each do | date | %>
  <tr>
   <td> <%= date.strftime("%d-%b-%Y") %> </td>
  </tr>
    <% @meetings.where(start_date: (date.to_time)..date.to_time+86399).each do | meeting | %>
      <tr>
        <td><%= meeting.name %></td>
        <td><%= meeting.start_date.strftime("%d-%b-%Y %I:%M %p") %></td>
        <td><%= meeting.end_date.strftime("%d-%b-%Y %I:%M %p") %></td>
        <td><%= meeting.duration %></td>
        <td><%= duration_display meeting.duration %></td>
      </tr>
    <% end %>
  </tbody>
  <% end %>
</table>
<br><br> 

enter image description here

Philip Wright
  • 306
  • 2
  • 3
  • Brilliant Philip just what i needed thank you very much for your help really appreciate it. – Sarah Duffy Sep 06 '21 at 11:10
  • You will want to order each day's entry: <% @meetings.where(start_date: (date.to_time)..date.to_time+86399).order("start_date").each do | meeting | %> – Philip Wright Sep 07 '21 at 12:27
  • Ah great you are a life saver. I also want to do the same but for the duration field that is calculated from (start date - end date) and then sum for each day. I am working on that now thanks a mill for your help – Sarah Duffy Sep 08 '21 at 18:58