Ruby on Rails using pluck to select singular dates from table?

346 Views Asked by At

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

1

There are 1 best solutions below

3
On BEST ANSWER

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