Rails 4 - ordering scopes - ruby-on-rails

Im trying to make an app in Rails 4.
I have two scopes in my qualifications model:
scope :completed, ->{ where(pending: !true) }
scope :pending, -> { where(pending: true) }
I am trying to list them (newest first) in my view.
I have this view file:
<% Qualification.pending.sort_by.year_earned.asc.each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.current_study %>
</div>
</div>
</div>
<% end %>
<% Qualification.completed.sort_by(&:year_earned).each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.completed_award %>
</div>
</div>
</div>
<% end %>
The second index works - but in the wrong order.
The first index - I have tried a million variations on the expression but can't find one that doesnt throw an error.
I have tried each of the above, and the following (each of which are following examples I found on this site):
<% Qualification.pending.sort_by(&:year_earned).reverse_order.each do |qualification| %>
<% Qualification.pending.sort_by(&:year_earned.reverse).each do |qualification| %>
<% Qualification.pending.sort_by('&:year_earned ASC').each do |qualification| %>
<% Qualification.pending.sort_by('year_earned ASC').each do |qualification| %>
Rather than list them all out - does anyone know how to list in ascending order?

I would include the order inside your scope.
scope :completed, ->{ where(pending: !true).order('year_earned DESC') }
scope :pending, -> { where(pending: true).order('year_earned DESC')}
And in your view just remove .sort_by and it should work then.
<% Qualification.pending.each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.current_study %>
</div>
</div>
</div>
<% end %>
<% Qualification.completed.each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.completed_award %>
</div>
</div>
</div>
<% end %>

I think you want Qualification.pending.order(year_earned: :asc).each do |qualification|
See this for more information: http://apidock.com/rails/ActiveRecord/QueryMethods/order

Related

Display last record in erb Rails with will_paginate

I am trying to display the last instanced record from my model Tribune with this layout :
Some random tribune
Some random tribune
Last
Last recorded tribune
I am using the gem will_paginate which allow me to display 10 tribunes / per page.
The issue is that the layout is working but applied to each page.
Every 10 tribunes, one is identified as "last". Obviously, I would like to have only one tribune identified as last.
Here is my code :
<div class="wrapping">
<% #tribunes.each do |tribune| %>
<div class="container">
<div class="mouse-out-container"></div>
<div class="row">
<% if tribune == #tribunes.last
%>
<h1>Last</h1>
<div class="col-xs-12 col-md-12">
<div class="card">
<div class="card-category">Popular</div>
<div class="card-description">
<h2><%= tribune.title %></h2>
<p><%= tribune.content.split[0...25].join(' ') %>...</p>
</div>
<img class="card-user" src="https://kitt.lewagon.com/placeholder/users/tgenaitay">
<%= link_to "", tribune, :class => "card-link" %>
</div>
<% else %>
<div class="col-xs-12 col-md-12">
<div class="card">
<div class="card-category">Popular</div>
<div class="card-description">
<h2><%= tribune.title %></h2>
<p><%= tribune.content.split[0...25].join(' ') %>...</p>
</div>
<img class="card-user" src="https://kitt.lewagon.com/placeholder/users/tgenaitay">
<%= link_to "", tribune, :class => "card-link" %>
</div>
</div>
<% end %>
<% end %>
</div>
<div class="center-paginate">
<%= will_paginate #tribunes, renderer: BootstrapPagination::Rails %>
</div>
</div>
</div>
When all Goole-fu fails, we have to dig in the source code. There we find some interesting methods:
# Any will_paginate-compatible collection should have these methods:
# current_page, per_page, offset, total_entries, total_pages
#
# It can also define some of these optional methods:
# out_of_bounds?, previous_page, next_page
From these, the method next_page looks interesting, as it seems to return nil if there are no more pages.
Now we can construct the loop:
<% #tribunes.each do |tribune| %>
<% if !#tribunes.next_page && tribune == #tribunes.last %>
<!-- We're on the last page and the last tribune of that page -->
Last tribune content
<% else %>
<!-- We still have tribunes to go -->
Normal tribune content
<% end %>
<% end %>

I'm trying to render two columns of information in my rails app

So, I have an app that is trying render 2 (or maybe more if needed) columns using a loop. It does one column and looks pretty good, but I want the option of 2 or even more. I know about "in_groups.of()", but I can't quite figure it out to work with my
<% #vendors.each do |vendor| %>
<%= link_to vendor do %>
<div class="row">
<div class="col-md-6">
<div class="card-container">
<div class="col-md-6">
<div class="card">
<%= image_tag attachment_url(vendor, :background_image), class: 'card-img-top' %>
<div class="card-block">
<h4 class="card-title"><%= vendor.Company %>. </h4>
<p class="card-text"><%= vendor.Description.html_safe.first(25) %></p>
<div class="card-standing"><strong><%= vendor.FinancialStanding %></strong></div>
</div>
</div>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
Not sure why you needed this, because everything can be managed with css and html. But you can make changes like this:
<% #vendors.to_a.in_groups_of(2).each do |vendor| %> # #vendors.to_a cover AR to array
<% vendor.each do |v| %> # becuase vendor is array here.
..........your code..............
<% end %>
<% end %>

Rails loop, link_to model_view

Rails: Need help looping through model array to link to the show page. I want to show the name, but link to the path. Seems like it should be simple but I have been coding all night and my brain is fried! please help.
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bars_path %>
<% end %>
</div>
</div>
</div>
</div>
This should work:
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bar.name, bar %>
</div>
</div>
<% end %>
</div>
</div>
You could also do <%= link_to bar.name, bars_path(bar) %>, but is prettier to just give the object. Rails will know which Url helper to use given a specific object.
Take a look at the UrlHelper documentation
Try this
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bar.name, bar_path(bar) %>
</div>
</div>
<% end %>
</div>
</div>

Create new Bootstrap row every third array item

I have an array of #schools (School.all), and I am displaying these #schools within a Bootstrap row (3 per row). I was wondering how I could make it so that for every third item in #schools, Ruby/Rails would create a new row and then repeat that process. Thanks.
<% for 3 in #schools %>
<div class="row">
<% #schools.each do |s| %>
<div class="col-md-4">
</div>
<% end %>
</div>
<% end %>
each_slice is your friend.
<% #schools.each_slice(3) do |schools| %>
<div class="row">
<% schools.each do |s| %>
<div class="col-md-4">
</div>
<% end %>
</div>
<% end %>
<% #schools.in_groups_of(3) do |schools| %>
<div class="row">
<% schools.each do |s| %>
<div class="col-md-4">
</div>
<% end %>
</div>
<% end %>
For documentation: http://apidock.com/rails/Array/in_groups_of

Conditional Rails Views

The code below is fast becoming a common theme in my rails app. I have a bunch of conditions in my view for handling empty data, as well as, managing the push and pull of my grid. This will only grow as I begin to add the 3 other status's. My questions is this. What is the best way of managing my grid elegantly either in the view or controller so my views don't become increasingly bloated with conditions?
<% if #jobs.where(status: 'published').size == 0 %>
<div class="row">
<div class="large-12 columns">
</div>
</div>
<% end %>
<% if #jobs.where(status: 'published').size == 1 %>
<div class="row">
<div class="large-4 push-8 columns">
</div>
</div>
<% elsif #jobs.where(status: 'published').size == 2 %>
<div class="row">
<div class="large-4 push-4 columns">
</div>
</div>
<% else %>
<% #jobs.in_groups_of(3, false) do |row| %>
<div class="row">
<% for job in row %>
<div class="large-4 medium-4 columns">
</div>
<% end %>
</div>
<% end %>
<% end %>
I think you should use switch case statement for managing multiple conditions.
#status = #jobs.where(status: 'published').size
<% case #status %>
<% when 0 %>
<div class="row">
<div class="large-12 columns">
</div>
</div>
<% when 1 %>
<div class="row">
<div class="large-4 push-8 columns">
</div>
</div>
<% when 2 %>
<div class="row">
<div class="large-4 push-4 columns">
</div>
</div>
<% else %>
<% #published_jobs.in_groups_of(3, false) do |row| %>
<div class="row">
<% for job in row %>
<div class="large-4 medium-4 columns">
</div>
<% end %>
</div>
<% end %>
<% end %>

Resources