# Find\_by\_sql error, any ideas please

**URL:** <https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501>\
**Category:** Rails\
**Created:** [January 23, 2009, 11:44pm UTC](https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501 "2009-01-23T23:44:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 23, 2009, 11:44pm UTC](https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501/1 "2009-01-23T23:44:09Z")

</div>

Hello,

I am having problems with the find\_by\_sql while trying to update a  
table. I am using mysql and rails 2.2.2.

A small test like this:

User.find\_by\_sql “update users set name=‘Test’”

generates a horrible error. I might also add that I have config.gem  
“mysql” in my environment.rb and that I have tested the update query  
with sqlite and it works. Any ideas?

Thanks,

Elías

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.all\_hashes  
/home/elioncho/.gem/ruby/1.8/gems/activerecord-2.2.2/lib/  
active\_record/connection\_adapters/mysql\_adapter.rb:564:in `select' /home/elioncho/.gem/ruby/1.8/gems/activerecord-2.2.2/lib/ active_record/connection_adapters/abstract/database_statements.rb:7:in`select\_all\_without\_query\_cache’  
/home/elioncho/.gem/ruby/1.8/gems/activerecord-2.2.2/lib/  
active\_record/connection\_adapters/abstract/query\_cache.rb:60:in  
`select_all' /home/elioncho/.gem/ruby/1.8/gems/activerecord-2.2.2/lib/ active_record/connection_adapters/abstract/query_cache.rb:81:in`cache\_sql’  
/home/elioncho/.gem/ruby/1.8/gems/activerecord-2.2.2/lib/  
active\_record/connection\_adapters/abstract/query\_cache.rb:60:in  
`select_all' /home/elioncho/.gem/ruby/1.8/gems/activerecord-2.2.2/lib/ active_record/base.rb:635:in`find\_by\_sql’

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 23, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501/2 "2009-01-23T23:53:13Z")

</div>

sounds like you should be using “execute” instead of “find\_by” since you  
aren’t trying to find anything.

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 24, 2009, 12:15am UTC](https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501/3 "2009-01-24T00:15:47Z")

</div>

Aldo S.,

Thanks for your reply, but the why does the update query inside the  
find\_by\_sql using sqlite works?

Kind regards,

Elías

On Jan 23, 5:53 pm, Aldo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 24, 2009, 12:17am UTC](https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501/4 "2009-01-24T00:17:33Z")

</div>

Sorry typo error. Here it is again:

Aldo S.,

Thanks for your reply, but then why does the update query inside the  
find\_by\_sql using sqlite works?

Kind regards,

Elías

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 25, 2009, 4:55am UTC](https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501/5 "2009-01-25T04:55:36Z")

</div>

elioncho wrote:

> User.find\_by\_sql “update users set name=‘Test’”

Use User.update\_all [‘name = ?’, ‘Test’]  
or User.update “update users set name=‘Test’”

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [January 24, 2009, 8:14am UTC](https://www.ruby-forum.com/t/find-by-sql-error-any-ideas-please/158501/6 "2009-01-24T08:14:51Z")

</div>

Should I use the execute method then?
