Link to home
Start Free TrialLog in
Avatar of wforester
wforester

asked on

Return the number of rows found in a table

I am trying to render the number of rows found in a table in my view but for some reason I am unable to get the output I expected. The pluralization is working because there are 2 rows counted but I cannot seem to get the number two to show up. The ouput from the cod below is

0 locations and it should be 2 locations

Here is what I have:
#In my controller:
def summary
 @locations = Location.count(:all, :conditions => ['user_id =?'
, current_user.id])
end
 
#in my view
<%= pluralize(@locations, 'location') %>

Open in new window

Avatar of ebriscoe100
ebriscoe100

I think you're missing a ".size". Try...

<%= pluralize(@locations.size, 'location') %>
Hi,

@locations is an array.  The pluralize helper method takes a count, and then a singular and optional plural spelling as found here: http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M001544

So, you are passing in an array, what you need to do is pass in the array's length, which is the count you are after in this case.  So do this instead:
<%= pluralize(@locations.length, 'location') %>

Open in new window

I just re read your question... I think the output you are after is:

2 locations

or

1 location

In that case, you need
<%= @locations.length -%> <%= pluralize(@locations.length, 'location') %> 
 
<%# Will produce: -%>
 
2 locations

Open in new window

Avatar of wforester

ASKER

Thanks for the reply's. However, now it gives a template error because the @locations object is nil. What I don't get now is when I take the length off of @locations I can see that the query is ran in the log. Is there another way I need to be approaching this?
ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.length) on line #2 of app/views/accounts/summary.html.erb:
1: <h1>Locations Reported</h1>
2: <%= @locations.length %> <%= pluralize(@locations.length, 'location') %>

Open in new window

You can test for nils using the "?" operator...

pluralize(@locations ? @locations.size : 0, 'location')
OK, now I get 0 locations, which is half way right. It has to be more than nil or it wouldn't say locations it would only say location... right?
OK... hang on a second.

This is getting too complex, I realized from your code above, in the controller, you are actually doing a count.  The count should return an integer.  It will be 0 or more.

So the code you posted at the very top is exactly correct.  You don't need to check for nil, and you don't need to call length.  Calling length on an integer will fail.

So something else is failing.

Are you sure there are records to find?

Try this code in your view and tell me what you get:



#In your controller:
def summary
 @locations = Location.count(:all, :conditions => ['user_id = ?', current_user.id])
end
 
#in my view
 
Total Count: <%= Location.count -%><br />
<%= pluralize(@locations, 'location') %><br />

Open in new window

That code returns

Total Count: 5
0 locations

This is why I have been banging my head against the computer monitor for an hour because I cannot figure out why it is zero. If I run the sql query directly it returns 2, not 0.
OK, so that means that the count you are doing on your locations table is returning zero.  That means that the count of locations for that user id is the locations = 0

Do you have any locations with the user id equal to the "current_user.id" ?

You can test this, again in the view, if you want like so:

#In your controller:
def summary
 @locations = Location.count(:all, :conditions => ['user_id = ?', current_user.id])
  @current_user_id = current_user.id
end
 
#in my view
 
Total Count: <%= Location.count -%><br />
Current User ID: <%= @current_user_id -%><br />
User IDs in Location table: <%= Location.find(:all).map {|loc| loc.user_id }.join(", ") -%><br />
<%= pluralize(@locations, 'location') %><br />

Open in new window

OK, Here is the details. When using your code I get the following.

Total Count: 5
Current User ID:
User IDs in Location table: 3, 3, 3, 1, 1
0 locations

Now here is where it gets confusing for me. current_user is available to any controller and view. So I added a if statement to the controller like so

if current_user.id == 1
 flash[:notice] = "This is 1"
end

The flash shows as intended. Also if I use just <%= current_user.id %> in the model it returns 1. Lastly if I modify the count statement like so.

@locations = Location.count(:all, :conditions => ['user_id = ?', '1']) it still shows 0 locations. What the heck am I doing wrong lol.
Try adding this "raise" this in your controller...

#In your controller:
def summary
 @locations = Location.count(:all, :conditions => ['user_id = ?', current_user.id])
  @current_user_id = current_user.id
raise  "locations: #{@locations.to_s} and user id: #{current_user.id}"
end

Do you get the expected values?
I get the following when I change it to that:

locations: 2 and user id: 1

That would be correct. Getting 2 locations when looking for current_user.id
So now that I am getting the proper results how do I get this to show in my view? This is the area that of ruby and rails that I am still a little behind on.
ASKER CERTIFIED SOLUTION
Avatar of raasdnil
raasdnil
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Grrrrr!!! I use this code and I get the following:

Current User ID: 1
0 Locations

This really annoying!!! For once my code is correct but I never get what I expect to get. LOL
Can you zip up your app directory and attach it.  Let me see what the problem is.
I finally figured it out! I was rendering my layout above the @locations object and this was keeping it from updating the view page properly.

Thank you again for all of your help and the explanations you gave!
Thank you again for your help.