I need to count all the microposts of my image where the status is equal to new.
<% for column in #images %>
<%= column.id %>
<%= column.microposts.count() %>
<% end %>
I have tried this line but it doesn't work:
<%= column.microposts.count(" WHERE status='new' ") %>
Is there a way I can add a condition ?
column.microposts.where(:status => 'new').count
column.microposts.find_all_by_status('new').count
Related
Just wondering how to dynamically name (say, in a loop) a series of instance variables. Something like this:
<% #current_issue.articles.each_with_index do |a, i| %>
<% i += 1 %>
<%= f.collection_select("article#{i}", #articles_hash1, :first, :last) %>
<% #articles1.each do |r| %>
<%= link_to(image_tag(r.image.url(:large)), r.image.url(:large), :id => 'article'+i.to_s+'_thumb'+r.id.to_s) %>
<% end %>
<% end %>
Where instead of #articles_hash1 it'd be #articles_hash[i]. I'm just not sure how to achieve that.
Cheers!
<% #current_issue.articles.each_with_index do |a, i| %>
<%= f.collection_select("article#{i}", #articles_hash[i], :first, :last) %>
<% #articles_hash[i].each do |r| %>
<%= link_to(image_tag(r.image.url(:large)), r.image.url(:large), :id => 'article'+i.to_s+'_thumb'+r.id.to_s) %>
<% end %>
<% end %>
I didn't check for any syntax issues, but this should do.
For anyone struggling with this, do yourself a favour and build an array of the instance variables in question, i.e.:
#articles = (1..3).to_a.map { |i| Article.all_articles(i).reverse }
And then loop through that array, i.e.:
<% #articles.each_with_index do |a, i| %>
<% i += 1 %>
<%= f.collection_select("article#{i}", #articles[i-1], :id, :name) %>
<% end %>
I have this piece of code that doesn't seem to work for a strange reason:
<% if (#user.photo.blank?) %>
<%= image_tag("empty_profile_pic.png") %> <!-- replace with user's image -->
<%else %>
<%= image_tag(#user.photo.url(:small)) %>
<% end %>
If the picture is null in the mySQL database I want it to display another pic.
I have tried, empty?, nil?, blank? but with no success, and also
#user.photo.blank.url.*
Any help?
How about
#user.photo.exists?
~Charles~
<%= image_tag(#user.photo.try(:url, :small) || "empty_profile_pic.png") %>
I'm using this in a view:
<% #line_items.group_by(&:year).each do |year, line_items| %>
<h2><%= year %></h2>
<% line_items.group_by(&:month).each do |month, lines| %>
<h3><%= Date.new(year.to_i, month.to_i, 1).strftime('%B') %> // <%= lines(&:quantity).sum %></h3>
<% for line in lines %>
<p><%= line.quantity %> <%= line.customer.name %></p>
<% end %>
<% end %>
<% end %>
I have a group of line items that have a :month, :year and :quantity attribute. I can group them by month and year with the above code, by I'm trying to total the quantity by month, too. Can't seem to figure that out. Getting the following error:
undefined method `lines' for #<#<Class:0x007fabb4f8afe0>:0x007fabb4f806d0>
You seem to be calling lines as if it were a method when in fact it is an array of objects. Try to do it like this instead:
<%= lines.map(&:quantity).sum %>
Map will create a new array by calling quantity on each record in the array. Then you can sum it.
I have a search controller and index page:
# search/index.html.erb
<% #user_prices.each do |up| %>
# This needs to go to particular ID of UserPrice.
<%= link_to show_user_price_path do %>
<%= up.product_name %>
<% end %>
<% end %>
----------------------------------------------------------
class SearchController < ApplicationController
def index
#search = UserPrice.search do
fulltext params[:search]
paginate(:per_page => 5, :page => params[:page])
end
#user_prices = #search.results
end
end
Above you can see on my search form I am trying to link the product name to that particular UserPrice.How would I do this?
Thanks.
<%= link_to up.product_name, show_user_price_path(up) %>
That should do?
Not exactly sure why you're using the block form of link_to, if you check the link_to documentation there is an easier way to use the function to get what you want.
<% #user_prices.each do |up| %>
<%= link_to up.product_name, show_user_price_path(up) %>
<% end %>
Noob question here :)
I'm testing a variable, and if it exists, I'd like to display an .each loop with a title.
Of course, the title should be displayed only once. Is there a way to do it? Any best practice?
<%
#twitter_friends.each do |u|
if #user = User.is_a_member?(u.id)
%>
# HERE I'D LIKE TO DISPLAY THE TITLE ONLY AT FIRST ITERATION
<% #user.name %> is your twitter friend, and is a member.
<% end %>
<% end %>
Thanks !
I would normally recommend using each_with_index and checking for a zero index, but seeing as you have a conditional in the loop, you should use a check variable like so:
<% shown_title = false %>
<% #twitter_friends.each do |u| %>
<% if #user = User.is_a_member?(u.id) %>
# HERE I'D LIKE TO DISPLAY THE TITLE ONLY AT FIRST ITERATION
<% unless shown_title %>
<h1>My Title</h1>
<% shown_title = true %>
<% end %>
<% #user.name %> is your twitter friend, and is a member.
<% end %>
<% end %>