How to get previous item in each loop - ruby-on-rails

I have a SQL query that loads information into a hash. I want to iterate through that information and output it into the view.
If the user_id is the same as the previous object's user_id, I don't want to display that user_id, just the name and everything else. It seems like the logic should be simple, but being a novice at Ruby and Rails, I'm not sure what is really available to do this.
I was trying something like this, but prev_id was never getting updated after the first iteration:
<% #session.each_with_index do |s, x| %>
<% if x == 0 then prev_id = 'nil' end %>
<% curr_id = s['id'] %>
<% if curr_id != prev_id %>
<%= s['id'] %>
<% end %>
<%= s['name'] %>
<%= s['count'] %><br>
<% prev_id = curr_id %>
<% end %>
Any help is greatly appreciated!

You have to declare your prev_id outside of the loop for it to persist between iterations:
<% prev_id = nil %>
<% #session.each_with_index do |s, x| %>
<% if x == 0 then prev_id = 'nil' end %>
<% curr_id = s['id'] %>
<% if curr_id != prev_id %>
<%= s['id'] %>
<% end %>
<%= s['name'] %>
<%= s['count'] %><br>
<% prev_id = curr_id %>
<% end %>

You should use chunk.
<% #session.chunk{|s| s["id"]}.each do |curr_id, a| %>
<%= curr_id %>
<% a.each do |s| %>
<%= s["name"] %>
<%= s["count"] %><br>
<% end %>
<% end %>

Related

There is a space between the elements rails?

As you can see below, I am having a problem of getting extra spaces in my search result. As you see in the photo, between the numbers and the elements, I am getting spaces and I do not why?
<td>
<% saved_element = ""%>
<% sensor.base_material.elests.each_with_index do |elest, v| %>
<% if elest.element.include? "O" %>
<% saved_element = elest %>
<% else %>
<%= elest.element.split('-').last %>
<% if elest.stoich != 1 %>
<sub><%= elest.stoich.to_i %></sub>
<% end %>
<% end %>
<% end %>
<% if saved_element.present? %>
<%= saved_element.element.split('-').last%>
<% if saved_element.stoich != 1 %>
<sub><%= saved_element.stoich.to_i %></sub>
<% end %>
<% end %>
</td>

Rails loop in html erb

How can I simplify the following lines:
<% if #campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.product_name %>
<% if #campaign.previous_campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.previous_campaign.product_name %>
<% end %>
<% end %>
I need to keep adding ".previous_campaign" until it is not present. So the next one in the above code would be:
<%= #campaign.previous_campaign.previous_campaign.previous_campaign.product_name %>
etc etc.
Something like this:
<% campaign = #campaign %>
<% while campaign.previous_campaign.present? %>
<% campaign = campaign.previous_campaign %>
<%= campaign.product_name %>
<% end %>
The code may need some debugging, but I guess the idea is clear
You could do something like this:
<% for c in #campaign do %>
<% if c.previous_campaign.present? %>
<%= c.previous_campaign.product_name %>
<% end %>
<% end %>

Get ActiveRecord value with outer index on Rails4

When I get List of Members in controller, I think #Members is array of ActiveRecord.
member_controller.rb
#members = Member.where(params[:param1])
So I know normally I write below to display.
member.html.erb
<% #members.each do |member| %>
<%= member.name %>
<% end %>
But in case of below, I would like to write index number at least loop 10 times like this.
<% (0..10).each do |idx| %>
<%= idx %>:
<%= members[idx].name %>
<% end %>
But it does not work. It can not be displayed members[idx].name
How can I make it?
Why don't you just use each_with_index method?
<% #members.each_with_index do |member, index| %>
<%= index %>
<%= member.name %>
<% end %>
If you need only 10 records, limit #members at the controller:
#members = Member.where(column: params[:column]).limit(10)
Does this help you?
Fix is
<% (0..10).each do |idx| %>
<%= idx %>:
<%= #members[idx].name %>
<% end %>
You forgot to use #. It should be #members, not members. And, if your query to the controller returns less than 10, then your looping way will crash. You will get the error,NoMethodError like NoMethodError: undefined method name' for nil:NilClass.
You can do as,
<% #members.each_index do |idx| %>
<%= idx %>:
<%= #members[idx].name %>
<% end %>
You can use each_with_index:
<% #members.each_with_index do |member, idx| %>
<%= idx %>:
<%= member.name %>
<% end %>
You could then use limit to limit the number of records returned.
Shouldn't it be
<% (0..10).each do |idx| %>
<%= idx %>:
<%= #members[idx].name %> #you forgot '#'here
<% end %>
Though each_with_index is a better option here
Thank you all. It worked!!
<% (0..10).each do |idx| %>
<%= idx %>:
<%= #members[idx].nil? ? '': #members[idx].name %>
<% end %>

undefined method `author' for nil:NilClass

I have some problems with my rails application.
<% #story.chapters.each do |chapter| %>
<% if round.to_s != chapter.round %>
<% if chosenChapterRound != chapter.round %>
<% maxVotes = 0 %>
<% end %>
<% if chapter.votes.count > maxVotes %>
<% maxVotes = chapter.votes.count %>
<% chosenChapterRound = chapter.round %>
<% chosenChapters[round.to_i] = chapter %>
<% end %>
<% end %>
<% end %>
<% chosenChapters.each do |chosenChapter| %>
<%= chosenChapter %>
<% end %>
this outputs something like this: #< Chapter:0x007fa1b8f59888>
What I really want to do is this:
<% chosenChapters.each do |chosenChapter| %>
<%= chosenChapter.author %>
<% end %>
But whenever I try to access one of the attributes inside of chosenChapter, I get a the following errormessage:
undefined method `author' for nil:NilClass
What am I doing wrong? Thanks!
At least one of your chosenChapters is nil. The problem is probably in the following fragment:
<% maxVotes = chapter.votes.count %>
<% chosenChapterRound = chapter.round %>
<% chosenChapters[round.to_i] = chapter %>

If Condition in each do Rails

Hi i need to print out just the candidates where active is == 0 here is my code in the view.
I can print if active is yes or no.. But in the each do loop i just want to print the active candidates.
So how can i add the condition to my each do loop, thanks.
<% #candidates.each do |candidate| %>
<div id="candidateper">
<div class="avatth" ><div class="avat_min">
<% if candidate.avatar.present? %>
<%= link_to (image_tag candidate.avatar.url(:thumb)), (candidate_path(candidate)) %>
<% else %>
<%= link_to (image_tag ("espanol/playersample.png")), (candidate_path(candidate)) %>
<% end %>
</div></div>
<div class="nameth"><%= candidate.name %></div>
<div class="activeth"><%= candidate.active ? t('generales.yess') : t('generales.noo') %></div>
<div class="generalth">
<% if candidate.user.purchased_at.present? %>
<%= candidate.user.purchase_defeated? ? t('generales.defeated') : t('generales.active') %>
<% else %>
<%= t('generales.noo') %>
<% end %>
</div>
<div class="actionsth"><%= link_to t('generales.show'), candidate_path(candidate) %>
<% if current_user.user_type == 'admin' %>
<%= link_to t('generales.delete'), candidate_path(candidate), method: :delete, data: { confirm: t('generales.delete_candidate_confirm') } %>
<% end %>
</div>
</div>
<% end %>
</div>
<% end %>
I`ve tried this
no luck syntax error on all my ideas :P
If candidate.active is actually a boolean then you could say:
<% #candidates.reject(&:active).each do |candidate| %>
...
<% end %>
If #candidates is actually an ActiveRecord::Relation then you could probably say:
<% #candidates.where(:active => false).each do |candidate| %>
...
<% end %>
to avoid pulling a bunch of stuff out of the database when you don't want it.
If active is actually a number (inside the database and outside the database) then you could say:
<% #candidates.select(&:zero?).each do |candidate| %>
...
<% end %>
or
<% #candidates.where(:active => 0).each do |candidate| %>
...
<% end %>

Resources