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!
Ok, i know what happened
i have a column named “Type”
and this makes everything happen
can anyone tells me more details about this ?
Thanks!
“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
Regards,
Jesse
2008/2/18, hei [email protected]:
Hi,
U may try this query
Message.find_by_sql(“select * from messages where id = ‘2’”).