Cameron Bass Cameron Bass - 7 months ago 9
Ruby Question

Hiding information in the database from the view. - Rails

I have a table right now that displays a bunch of attributes of the User. Here is my problem. Each person has the ability to sign up to a group but in some edge cases a person can get in the table without being signed up to a group. for the sake of clarity for the users reading the table I'd like to simply hide each person that is not signed up to a group from the table. This is a bit more tricky than I anticipated. Ill show some code and a screenshot for better understanding.

enter image description here

as you can see if a person has no group don't display in table.

Im trying to make a method and call it on the controller rendering the table. The Person and Group are two different models with an association of HABTM. How do I say "if person has no groups don't display in table" in ruby?

def index
@people = Person.order(sort_column => sort_direction)
@groups = Group.all
end


This is the controller that is making the table

<table class="table table-striped">
<thead>
<tr>
<th><%= sortable 'phone_number', 'Phone Number'%></th>
<th><%= sortable 'subscribed', 'Subscribed'%></th>
<th><%= sortable 'city' %></th>
<th><%= sortable 'state' %></th>
<th><%= sortable 'zip' %></th>
<th><%= sortable 'country' %></th>
<th><%= sortable 'created_at', "Joined" %></th>
<th>Groups</th>

</tr>
</thead>
<tbody>
<% @people.each do |person| %>
<tr>
<td><%= person.phone_number %></td>
<td><%= person.subscribed? ? "Yes" : "No" %></td>
<td><%= person.city %></td>
<td><%= person.state %></td>
<td><%= person.zip %></td>
<td><%= person.country %></td>
<td><%= time_ago_in_words person.created_at %> ago</td>
<td><%= person.groups.order(:id).pluck(:name).to_sentence %></td>
</tr>
<% end %>
</tbody>




group model

class Group < ActiveRecord::Base
has_and_belongs_to_many :people
has_and_belongs_to_many :messages
end


perons model

class Person < ActiveRecord::Base
has_many :deliveries
has_and_belongs_to_many :groups

Answer

These options are O(n^2) and pull in quite a bit into memory so as long as you're ok with that, these are easy to implement:

You have two options, one for your view:

<% @people.each do |person| %>
  <% next if person.groups.blank? %>
  <tr>
    <td><%= person.phone_number %></td>
    <td><%= person.subscribed? ? "Yes" : "No" %></td>
    <td><%= person.city %></td>
    <td><%= person.state %></td>
    <td><%= person.zip %></td>
    <td><%= person.country %></td>
    <td><%= time_ago_in_words person.created_at %> ago</td>
    <td><%= person.groups.order(:id).pluck(:name).to_sentence %></td>
  </tr>
<% end %>

Or you could filter in your controller:

@people = Person.order(sort_column => sort_direction).to_a.keep_if { |person| person.groups.present? }
Comments