Array.count works fine locally but breaks on heroku - ruby-on-rails

Right now, if I go to the index action of a model that I have, I don't show the basic table of data that rails generates for me if there are no existing records in the database. I do something like:
<% if #my_records.count > 0 %>
<table>
<tr>
<th>...</th>
<th>...</th>
<th>...</th>
<th>etc</th>
</tr>
<% #my_records.each do |my_record| %>
<tr>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
</tr>
<% end %>
</table>
<% end %>
This works locally. However, when I push my app to heroku, this causes a 500 error and the log says:
ActionView::TemplateError (undefined method 'count' for []:Array) on line ...
So I change it to .length and it works fine. Can anyone tell me why that is? Someone has told me that these were redundant and rails got rid of .count but my understanding was that .length is an Array function that tells you how many items are in the Array and .count was an ActiveRecord method for determining how many items in the array were actual records in the database.
Can anyone shed light on this for me?

That's ruby issue, not rails. Locally you probably have 1.8.7, and heroku has 1.8.6. The Enumerable#count method was introduced in 1.8.7: compare http://ruby-doc.org/core-1.8.6/classes/Enumerable.html and http://ruby-doc.org/core-1.8.7/classes/Enumerable.html.

Related

Adding Time and Date to Paper_Trail data

New to Rails, but learning a ton.
I'm using Paper Trail to save versions of my database, focusing on one sum that I want to maintain with each update. The sum is called "xoi_qb".
Within my model, I have defined the data I want to save like this:
def get_xoi_qb
xoi_qb = []
self.versions.each do |version|
unless version.reify.nil?
xoi_qb << version.reify.xoi_qb
end
end
return xoi_qb
end
Within my the html page that I want to display this data, I have this:
<p>Previous XOI</p>
<table summary="XOI Versions">
<tr>
<td><%= #quarterback.versions.map { |version| version.reify.xoi_qb} %></td>
</tr>
This is all working properly.
Alongside the "Previous XOI" data, I want to display the time and date that the "xoi_qb" sum is updated (I'm assuming by using "updated_at", but nothing I've tried is working).
Really appreciate your help.
(Side question: my "XOI" sum is appearing with the brackets (i.e. [745]) on the website...if you can tell me how to get rid of that, it would be awesome.)
I believe the date and time of the version is stored in the created_at field of the version. Did you try that?
As of your side question: you're basically doing [745].to_s in your view code, i.e. casting an array to a string, and it does what you're asking it to do. Your code looks like your intention was to output all the previous versions. For this to work you would rather iterate the array than convert it to a string. Something like:
<table>
<% quarterback.versions.each do |version| %>
<tr>
<td><%= version.reify.xoi_qb %></td>
</tr>
<% end %>
</table>
... And with dates:
<table>
<% quarterback.versions.each do |version| %>
<tr>
<td><%= version.reify.xoi_qb %> dated <%= version.created_at %></td>
</tr>
<% end %>
</table>

Sequence for the instance variable in rails view

In the Rails view users/index.html.erb
I iterated over each users to list them all.
Want sequence no. in front of the user list. I have used each_with_index to achieve this.
I want to know if rails provides any better alternate to this. I am using rails 4.1
<% #users.each_with_index do |user,count| %>
<tr>
<td><%= count+1 -%></td>
<td><%= user.name %></td>

Rails - how do you access a foreign key object in a html.erb template? Works in console

I am learning rails by trying to model a collectible card game.
I have a champion model and a rarity model. I have the has_many/belongs_to in the model definition and this works in the console:
c = Champion.find(1)
c.rarity.name
=> "Uncommon"
When I do the same thing in a template, I get
<%= champion.rarity.name %>
undefined method `name' for nil:NilClass
Any ideas on how to get this to work?
This is on Rails 3.2.2.
Update: Full .erb code
<% #champions.each do |champion| %>
<tr>
<td><%= champion.name %></td>
<td><%= champion.rarity.name %></td>
</tr>
<% end %>
If every Champion does not have a Rarity association (some are nil), you can use a .try() to print the name. Otherwise the .each will fail with a NoMethod when one without a Rarity is encountered.
<% #champions.each do |champion| %>
<tr>
<td><%= h champion.name %></td>
<td><%= h champion.rarity.try(:name) %></td>
</tr>
<% end %>
Or the less clever unless nil method:
<% #champions.each do |champion| %>
<tr>
<td><%= h champion.name %></td>
<td><%= h champion.rarity.name unless champion.rarity.nil? %></td>
</tr>
<% end %>
Note: I have also added the h() helper method to encode these for HTML output, though this is done automatically in Rails 3.

Handling Yahoo Finance Quotes Hash in Rails 3

I am trying to manipulate the hash returned from Yahoo Finance for their Standard Quote using Ruby on Rails. I am new to Ruby and am getting a compile error in a view .erb file when I try to run the program. My objective is relatively straightforward - I want to display the Stock symbol, Bid and Ask prices and Corp name for each quote contained in the hash.
I stored the hash in an instance variable called #quote_info and pass this hash to the View.
The code in the view is as folows :
<h1>Stock Quote from Yahoo Finance</h1>
<p>Stock Symbol(s) Requested: <%= #quote_list %> </p>
<table>
<tr>
<th>Symbol</th>
<th>Bid Price</th>
<th>Ask Price</th>
</tr>
<% #quote_info.each |stock| do %>
<tr>
<td><%= stock.symbol %></td>
<td><%= stock.bid %></td>
<td><%= stock.ask %></td>
</tr>
<% end %>
</table>
I get a compile error on the each statement line , pointing to after the do term.
compile error
/home/lvl9/waf_projects/squotes_app/app/views/screenquotes/show.html.erb:18: syntax error, unexpected kDO
');#output_buffer.append_if_string= #quote_info.each |stock| do
^
Any thoughts would be much appreciated. I am tearing my hair out and cannot afford to lose any more.
Just a misplaced do:
<% #quote_info.each do |stock| %>

how do i fix undefined method `user_id' for nil:NilClass?

i am trying to start my server after a db:migrate:reset and suddenly my SQlite3 server will not start. I get the error: ActionView::Template::Error (undefined method 'user_id' for nil:NilClass) when the server begins to render my datum/index page.
Before i did this i had actual prices in my database so the user_id could be detected and everything worked but now since the prices are gone i believe its giving this error.
Controllers - Datum & Price:
def index
#prices = Price.all
end
Views - datum/index & prices/index:
<h1>Prices</h1>
<table>
<tr>
<th>User</th>
<th>Date</th>
<th>Price name</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #prices.each do |price| %>
<tr>
<td><%= price.user_id %></td>
<td><%= price.date %></td>
<td><%= price.price_name %></td>
<td><%= price.price %></td>
<td><%= link_to 'Show', price %></td>
<td><%= link_to 'Edit', edit_price_path(price) %></td>
<td><%= link_to 'Delete', price, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Price', new_price_path %>
I think i am doing this the wrong way as i am new to Rails. My goal was to duplicate my prices/index view so my datum/index is the same so i could then give both unique looks. How do i correct this issue and am i doing this correctly?
I'm guessing that you don't know what rake db:migrate:reset does. There's no description string for it so don't ask rake what it does, you have to look at the source:
# desc 'Resets your database using your migrations for the current environment'
task :reset => ['db:drop', 'db:create', 'db:migrate']
So rake db:migrate:reset destroys your database (including any data you had in it), recreates it, and then applies the migrations to bring everything up to date again. But, all your original data is still gone.
The db:drop part of db:migrate:reset probably explains why you're getting nil all over the place. However, you should be getting an empty array from Price.all if all your data was gone so perhaps you've added something after your reset.
Weird, i think it was giving this error because i was using AptanaStudio 3 with the git terminal. I just restarted everything and it started working now as if the Database needed time to refresh itself. So in conclusion just restart everything and see if it works then.

Resources