Totaling and grouping records in a Rails 3 app - ruby-on-rails-3

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.

Related

How do I create a new ul when crime name equals to 10

Count Crimes
if the list of crimes is equal to 10 on 11 create a new list, This is the updated version of it sorry....
<ul>
<% category.crimeheaders.each do |crimeheader| %>
<% crimeheader.crimes.each do |crime| %>
<li>
<%= crime.id %>
</li>
<li>
<%= crime.name %>
</li>
<% end %>
<% end
%>
How would i do this???
Ok, first you need to get the list of all crimes, regardless and not structured by crimeheader.
One way to do this is to add a relation to crimes at the category level, through the crimeheaders relation:
class Category
has_many :crimeheaders
has_many :crimes, through: :crimeheaders # this is what you add
...
end
Once you have this, you can loop through all crimes in a category, in batches of 10.
<% category.crimes.each_slice(10) do | batch | %>
<ul>
<% batch.each do |crime| %>
<li><%= crime.name %></li>
<% end %>
</ul>
<% end %>
That will get all the crimes for the category, then give you slices of 10 at a time to display in lists.
Now, if you want to get clever, you can actually retrieve from the db in batches of 10.
Probably not worth it, but here is the idea:
<% Crime.where(category_id: 123).find_in_batches( batch_size: 10 ) do | batch | %>
<ul>
<% batch.each do |crime| %>
<li><%= crime.name %></li>
<% end %>
</ul>
<% end %>

Why is my index page dumping table data at the end of the list?

I am getting this output when I got to the /projects, which should just list projects and an associated link:
I cannot figure out why all the information from the projects table is being dumped after the list.
Controller index method:
def index
#projects = Project.all
end
index.html.erb:
<%= #projects.each do |project| %>
<li>
<%= link_to project.title, projects_path(project) %>
<%= link_to project.link, project.link %>
</li>
<% end %>
Any ideas to why this is happening?
Thanks for reading!
Change the <%= on your loop to <%:
<% #projects.each do |project| %>
What it's doing now is executing the loop and printing all your li elements, and then also printing the contents of #projects (because of the <%=).

How to check if an ActiveRecord table entry is empty

I am not sure if the title of this question uses proper jargon, but hopefully this description will help. If you need more information, please let me know.
I am taking Chinese text from a form and splitting it into a 2d array of sentences and words. I then want to define all the words using dictionary entries in my database. Some of the words aren't in the database, so I want to check for that. What I am trying isn't working.
Here is my current code:
<% #lesson.parsed_content.each_with_index do |sentence, si| %> #iterate 1st dimension
<% sentence.each_with_index do |word,wi| %> #iterate 2nd dimension
<% sentence = sentence.to_s %>
<div class="word blt" id="<%= wi %>">
<div class="definition blt">
<% definition = DictionaryEntry.where(:simplified => word) %> #search by simplified chinese
<% definition.find_each do |w| %>
<% if w.definition == nil %> # PROBLEM: this never returns true.
<%= word %>
<% else %>
<%= w.definition %>
<% end %>
<% end %>
</div>
<div class='chinese blt'> <%= word %></div>
</div>
<% end %>
<% end %>
How can I change <% if w.definition == nil %> to return true if there is no definition in my database?
This is a shot in the dark but first I would switch your code around when you are converting the variable sentence to a string and looping through it. (unless you have a reason for it being that way)
<% sentence = sentence.to_s %>
<% sentence.each_with_index do |word,wi| %> #iterate 2nd dimension
Second, depending on how your data was put inside the database it might be an empty string instead of nil. So I would change the condition from
<% if w.definition == nil %> # PROBLEM: this never returns true.
to
<% if w.definition.blank? %> # Checks to see if definition is blank
Blank will check if its false, empty, or a whitespace string.
Finally, indentation is helpful especially when running loops and conditionals. It's easier on the eyes and helps you understand what's going on.
<% #lesson.parsed_content.each_with_index do |sentence, si| %>
<% sentence = sentence.to_s %>
<% sentence.each_with_index do |word,wi| %>
<div class="word blt" id="<%= wi %>">
<div class="definition blt">
<% definition = DictionaryEntry.where(:simplified => word) %>
<% if definition.empty? %>
<% word %>
<% else %>
<% definition.find_each do |w| %>
<%= w.definition %>
<% end %>
<% end %>
</div>
<div class='chinese blt'> <%= word %></div>
</div>
<% end %>
<% end %>
Let me know the results.

Rails: dynamically naming instance variables

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 %>

Ruby on Rails - SQL Query count

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