Apologies if I am going about the wring way for this. But I have the following code and want to have a sum of it rather than two separate numbers:
<% #all_users.each do |i| %>
<%= i.liked_recipes.count %>
<% end %>
The output for this code is as follows:
12 4
What I would like the output to be would be the sum:
16
Also:
<%= #all_users.sum { |u| u.liked_recipes.count } %>
You need some way to keep track of the count:
<% total = 0 %>
<% #all_users.each do |i| %>
<% total += i.liked_recipes.count %>
<% end %>
<%= total %>
A shorter way of doing this would be to use inject
#all_users.inject(0) { |result, i| result + i.liked_recipes.count }
If #all_users is not loaded other than for these recipe counts then it is probably best to let the database do the counting:
LikedRecipe.where(:user_id => #all_users.select(:id)).count
Related
In my rails app I have below code to print some data from database.
<% #di.each do |d| %>
<% if d["visibility_team"] == 'for_all' %>
//my code goes here
<% end %>
<% end %>
I just want to print first 3 occurrence which fulfill the d["visibility_team"] == 'for_all' condition.
How can I do this?
If you can't get #di as 3 records from DB, how about keeping counter how many ds were printed?
Something like this (feel free to style it the way you want)
<% counter = 0 %>
<% #di.each do |d| %>
<% if d["visibility_team"] == 'for_all' %>
<% counter += 1 %>
<% break if counter == 3 %>
//your code goes here
<% end %>
<% end %>
However it's usually a bad taste to have so much logic in views.
I'm looking to get the last 5 items in an array, but to display each item individually, not as a group.
Here's what I've tried below with no success.
#array = Town.all
<% #array.each_slice(5) do |a| %>
<%= a.first %>
<%= a.second %>
<%= a.third %>
<%= a.fourth %>
<%= a.fifth %>
<% end %>
I can't figure out how to pull out a specific number of items and then to call each individual item.
The method you are looking for is in_groups_of (https://apidock.com/rails/Array/in_groups_of):
<% #towns.in_groups_of(5) do |five_towns| %>
<% five_towns.each do |town| %>
# [...]
<% end %>
<% end %>
Update: Maybe you want only the last 5 elements of the array. If so, you can simply call:
Town.all.last(5)
# => array of (max) 5 Town records
To get the last 5 items of an Array you could use last and then iterate them with each:
<%= #array.last(5).each do |a| %>
...
<% end %>
But it seems that you want to get the last 5 records of a model; in that case, you could try this instead:
#array = Town.limit(5).order('id desc')
<%= #array.each do |a| %>
...
<% end %>
... is there a way that I can have let's say the last 5 records of a
model all within one loop? maybe
<%= #array.each do | a, b, c, d, e| %>
<%= a.description %>
<%= b.description %>
<% end %>
You could use the index of each item instead of using each; for example:
#last_five = #array.last(5)
<%= last_five[0] %>
<%= last_five[1] %>
<%= last_five[2] %>
<%= last_five[3] %>
<%= last_five[4] %>
Although that wouldn't be DRY, so it will be better to stick with each.
The following loop goes through the sales column and lists all 4 existing product values, like 19.99 19.99 3.99 3.99 to the corresponding user id.
<% #sales.each_with_index do |sale, index| %>
<% if current_user.id == sale.user_id %>
<% price = Warehouse.where(:product => sale.product).pluck(:mrr) %>
<%= value = price.split(',').join('.').to_f %>
<% else %>
<% end %>
Now I want to save the results/values into a new global variable and add up each out of "value". So the result of 19.99 19.99 3.99 3.99 should be 47.96.
I'm completely lost. Any Ideas?
You could do something like this:
<% total = 0 %>
<% #sales.each_with_index do |sale, index| %>
<% if current_user.id == sale.user_id %>
<% price = Warehouse.where(:product => sale.product).pluck(:mrr) %>
<%= value = price.split(',').join('.').to_f %>
<% total += value %>
<% end %>
<% end %>
<%= "Total is #{total}" %>
It is highly questionable to have code like this in the view though. You could get prices and calculate totals in your controller instead.
Also note that you are missing an end. I changed the unneeded else to an end.
In your controller you can create a instant variable prefixed by # so it can be used throughout your view
For example in your controller
#total_value = 0
And in your view
<%#sales.each_with_index do |sale, index| %>
<% if current_user.id == sale.user_id %>
<% price = Warehouse.where(:product => sale.product).pluck(:mrr) %>
<%= value = price.split(',').join('.').to_f %>
<% #total_value += value %>
<% else %>
<% end %>
You shouldn't add that kind of logic in your view. Create a view object class (that the controller instantiates) too handle all of this. You also probably can do something like:
user.sales.each do |sale|
total += find_price(sale)
# do more stuff
end
If you are asking 'if current_user.id == sale.user_id' then you most likely doing it wrong.
In that view object you could have a hash that has all the prices you want to show and iterate over that in your view.
In code:
<% #offers.each do |offer|%>
<%= offer.percentageOff%> <b>% OFF on order of</b>
<%= image_tag('1407951522',:size=>'10x10',:alt=>'Rs.')%>
<%= offer.amountforDiscount%>
<%= button_to 'Delete',{:action=>'destroy',:id=>offer.id},class: "" %>
<% end %>
I am new in rails. I want to show all the offers in a numbered list, I can't use database table because id is not in order. For example:
30 % OFF on order of Rs.2000
13 % OFF on order of Rs.1000
How do i achieve this?
Easiest way if you don't want to use each_with_index then You can define variable #count=0 and display, as loop occur it will be increment by 1
<% #count = 0 %>
<% #offers.each do |offer|%>
<%= #count += 1 %> #I guess you want this to show Sr.No
...... # your code
<% end %>
each_with_index way :
<% #offers.each_with_index do |offer, index|%>
<%= index + 1 %> # index starts from 0, so added 1 to start numbering from 1
...... # your code
<% end %>
Is this the DRYest way to do it in ruby?
<% for item in #items %>
<%= n = n + 1 rescue n = 1 %>
<% end %>
which initializes "n" to '1" and increments it as the loop progresses (and prints it out) since this is in one my app's views
You can use a ternary operator:
<% for item in #items %>
<%= n = n ? n+1 : 1 %>
<% end %>
But, depending on what you're trying to do, I'm guessing an each_with_index would be more appropriate
<% #items.each_with_index do |item, n| %>
<%= n %>
<% end %>
You could also rely on ruby's nil coercion to an integer which results in zero.
<% for item in #items %>
<%= n = n.to_i + 1 %>
<% end %>
Um.
n = #items.size