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

Ruby

Avatar of undefined
Last Comment
wforester

8/22/2022 - Mon
ebriscoe100

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

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

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

raasdnil

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
wforester

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

ebriscoe100

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

pluralize(@locations ? @locations.size : 0, 'location')
ASKER
wforester

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
raasdnil

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

ASKER
wforester

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.
raasdnil

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
wforester

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.
ebriscoe100

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?
ASKER
wforester

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
wforester

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
raasdnil

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
wforester

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
raasdnil

Can you zip up your app directory and attach it.  Let me see what the problem is.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
wforester

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!
ASKER
wforester

Thank you again for your help.