# Problem with find\_by\_sql()

**URL:** <https://www.ruby-forum.com/t/problem-with-find-by-sql/148421>\
**Category:** Rails\
**Created:** [September 12, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/problem-with-find-by-sql/148421 "2008-09-12T17:38:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gismosio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gismosio](https://www.ruby-forum.com/u/gismosio)\
**Post date:** [September 12, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/problem-with-find-by-sql/148421/1 "2008-09-12T17:38:09Z")

</div>

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 😃

thanks  
bye

---

<div class="post-metadata">

**Author:** ![gismosio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gismosio](https://www.ruby-forum.com/u/gismosio)\
**Post date:** [September 12, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/problem-with-find-by-sql/148421/2 "2008-09-12T18:37:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gismosio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c2a13f/32.png) [@gismosio](https://www.ruby-forum.com/u/gismosio)\
**Post date:** [September 13, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/problem-with-find-by-sql/148421/3 "2008-09-13T16:37:14Z")

</div>

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…
