Skip to content

Instantly share code, notes, and snippets.

@EminenceHC
Last active December 29, 2015 14:29
Show Gist options
  • Save EminenceHC/7684452 to your computer and use it in GitHub Desktop.
Save EminenceHC/7684452 to your computer and use it in GitHub Desktop.
Query in model?
def index
@users_array_id = User.where(loginable_type: 'Counselor').includes(:loginable).all.map { |u| [u.full_name, u.loginable.id] }.uniq
runquery = Counselor.joins(:user)
runquery = runquery.where(:users => {:first_name => params[:first_name]}) if params[:first_name].present?
runquery = runquery.where(:users => {:last_name => params[:last_name]}) if params[:last_name].present?
runquery = runquery.where(:users => {:loginable_id => params[:id]}) if params[:id].present?
runquery = runquery.where(:users => {:active => params[:active]}) if params[:active].present?
runquery = runquery.select('counselors.*, users.*')
@query = runquery
end
<% if is_student == false %>
<div class="well" style="float:left;padding-bottom:0px;">
<%= simple_form_for(counselors_path, :method =>'get', :html => { :class => 'form-horizontal' }) do %>
<!--
<%= text_field_tag :first_name, nil, :placeholder => 'First Name', style: 'margin:0px 0px 5px 0px;' %>
<%= text_field_tag :last_name, nil, :placeholder => 'Last Name', style: 'margin:0px 0px 5px 0px;' %>
-->
<%= select_tag 'id', options_for_select(@users_array_id), { :prompt => 'Name', class: 'chosen-select', style: 'margin:0px 0px 5px 0px;'} %><br>
<%= select_tag 'active', options_for_select('True'=>'t', 'False'=>'f'), { :prompt => 'Active', style: 'margin:5px 0px 5px 0px;'} %>
<%= submit_tag 'Submit', :class => 'btn btn-primary', style: 'margin:5px 0px 5px 0px;' %>
<% end %>
</div>
<div class="well" style="float:right;">
<p><%= link_to " New Counselor", new_counselor_path, :class => 'btn btn-success glyphicon glyphicon-plus' %></p>
</div>
<div class="well" style="float:right;padding:3px 5px 0px 5px;margin:0px 10px 0px 20px;">
<p><input type="checkbox" name="basic" class="checkbox" onclick="saveToStorage()" /> Basic</p>
<p><input type="checkbox" name="address" class="checkbox" onclick="saveToStorage()" /> Address</p>
<p><input type="checkbox" name="phone" class="checkbox" onclick="saveToStorage()" /> Phone</p>
</div>
<div class="tableholder table-responsive">
<table id="report" class="table table-condensed table-bordered table-striped table-responsive">
<tr>
<th>First</th>
<th class="basic">Middle</th>
<th>Last</th>
<th class="basic">UID</th>
<th>Email</th>
<th class="basic">Social</th>
<th class="basic">Sex</th>
<th class="basic">DOB</th>
<th class="address">Address</th>
<th class="address">City</th>
<th class="address">State</th>
<th class="address">Zip</th>
<th class="phone">Home</th>
<th class="phone">Work</th>
<th class="phone">Mobile</th>
<th class="basic">Hire Date</th>
<th>Edit</th>
<th>Status</th>
</tr>
</thead>
<% @query.each do |s| %>
<tbody>
<tr>
<td><%= s.first_name %></td>
<td class="basic"><%= s.middle_name %></td>
<td><%= s.last_name %></td>
<td class="basic"><%= s.id %></td>
<td><%= s.email %></td>
<td class="basic"><%= s.social_security_no %></td>
<td class="basic"><%= s.sex %></td>
<td class="basic"><%= s.date_of_birth %></td>
<td class="address"><%= s.street_address %></td>
<td class="address"><%= s.city %></td>
<td class="address"><%= s.state %></td>
<td class="address"><%= s.postal_code %></td>
<td class="phone"><%= s.home_phone %></td>
<td class="phone"><%= s.work_phone %></td>
<td class="phone"><%= s.mobile_phone %></td>
<td class="basic"><%= s.hire_date %></td>
<td><%= link_to " Edit", edit_counselor_path(@counselor, id: s.loginable_id), class: 'btn btn-mini glyphicon glyphicon-pencil' %></td>
<td><% if s.active? %>
<%= link_to "Active", toggle_active_off_counselor_url(id: s.id), class: 'btn btn-mini btn-success', data: {confirm: 'Are you sure you want to deactivate user?'} %>
<% else %>
<%= link_to "Deactive", toggle_active_on_counselor_url(id: s.id), class: 'btn btn-mini btn-danger', data: {confirm: 'Are you sure you want to activate user?'} %>
<% end %>
</td>
</tr>
</tbody>
<% end %>
</table>
</div>
<% else %>
<h2>You are not authorized to view this page.</h2>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment