# Strange thing with find\_by\_sql in oracle

**URL:** <https://www.ruby-forum.com/t/strange-thing-with-find-by-sql-in-oracle/129199>\
**Category:** Rails\
**Created:** [February 15, 2008, 10:57am UTC](https://www.ruby-forum.com/t/strange-thing-with-find-by-sql-in-oracle/129199 "2008-02-15T10:57:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 15, 2008, 10:57am UTC](https://www.ruby-forum.com/t/strange-thing-with-find-by-sql-in-oracle/129199/1 "2008-02-15T10:57:11Z")

</div>

I did two sql queries in the same environment and at the same time:  
First:  
Message.find\_by\_sql([“select \* from messages where id = ?”, 1])  
Returns:  
gumentError: wrong number of arguments (0 for 1)  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/  
active\_record  
ase.rb:1750:in `y' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ active_record ase.rb:1750:in`compute\_type’  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/  
active\_record  
ase.rb:1316:in `instantiate' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ active_record ase.rb:532:in`find\_by\_sql’  
from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/  
active\_record  
ase.rb:532:in `collect!' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ active_record ase.rb:532:in`find\_by\_sql’  
from (irb):11  
Second:  
Message.find\_by\_sql([“select \* from messages where id = ?”, 2])  
Returns:  
=\> []  
In table messages, there is a message with id 1, but no message with  
id 2  
also, I did this query with another table users, all worked well  
My environment:  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]  
Rails 2.0.2  
database: oracle 10  
table messages has more columns than table users …  
Can anyone help me? Thanks!

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 18, 2008, 3:04am UTC](https://www.ruby-forum.com/t/strange-thing-with-find-by-sql-in-oracle/129199/2 "2008-02-18T03:04:30Z")

</div>

Ok, i know what happened  
i have a column named “Type”  
and this makes everything happen  
can anyone tells me more details about this ?

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 18, 2008, 9:29am UTC](https://www.ruby-forum.com/t/strange-thing-with-find-by-sql-in-oracle/129199/3 "2008-02-18T09:29:11Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 18, 2008, 6:54am UTC](https://www.ruby-forum.com/t/strange-thing-with-find-by-sql-in-oracle/129199/4 "2008-02-18T06:54:19Z")

</div>

“type” is a preserved keyword in ActiveRecord for implementing “Single  
table  
inheritance”, so don’t use it as a table column name.

See  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/base.rb:302  
:

# == Single table inheritance

# 

# Active Record allows inheritance by storing the name of the class in

a  
column that by default is named “type” (can be changed

# by overwriting Base.inheritance\_column). This means that an

inheritance looking like this:

# 

# class Company \< ActiveRecord::Base; end

# class Firm \< Company; end

# class Client \< Company; end

# class PriorityClient \< Client; end

# 

# When you do Firm.create(:name =\> “37signals”), this record will be

saved  
in the companies table with type = “Firm”. You can then

# fetch this row again using Company.find(:first, "name =

‘37signals’")  
and it will return a Firm object.

# 

# If you don’t have a type column defined in your table, single-table

inheritance won’t be triggered. In that case, it’ll work just

# like normal subclasses with no special magic for differentiating

between  
them or reloading the right type with find.

# 

# Note, all the attributes for all the cases are kept in the same

table.  
Read more:

# [P of EAA: Single Table Inheritance](http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html)

# 

- 

Regards,  
Jesse

2008/2/18, hei [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 18, 2008, 10:09am UTC](https://www.ruby-forum.com/t/strange-thing-with-find-by-sql-in-oracle/129199/5 "2008-02-18T10:09:13Z")

</div>

Hi,

U may try this query

Message.find\_by\_sql(“select \* from messages where id = ‘2’”).
