Problem with find_by_sql()

hi…
I have a question concerning the method find_by_sql()…

i wonna to make a query like this:
“SELECT category, COUNT(*) FROM tables1”
but cannot return anything…
I want to know the correct procedure for the output in view…
In my trials I’ve found this:

######################################################
NoMethodError in Blog#index

Showing blog/index.html.erb where line # raised:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.title
######################################################

please help me…i’m a newbies :smiley:

thanks
bye

Hello Gabriele,

Are you sure there is some data to return from the table? Check that
the find_by_sql call returns nil or has an array.

Why not skip the find_by_sql and use an ActiveRecord calculation
method? example:

category_counts = MyModelThatUtilizesTables1.count( :all,
:group => [‘category’],
:order => [‘category’])

You’ll get back an array to process in your view. You can add
a :conditions argument too.

Then in your view check category_counts.nil? or category_counts.size
== 0 (can’t remember if it returns nil or an empty array)

-Jim

thanks so much…
but i wonna create a menu with all categories in tables…
for example:
Bio (3) – numeber 3 are articles in table…with category = bio
Ruby (10) – same as above but with category = ruby

ok?
In PHP I can have this result with a query…
I want this with RoR…

With your method is possible?

thanks
Gabriele
ps: I’m sorry for my english…