find_by_sql returing a class and not an instance of a class?

T

Tom

Hi everyone,

I'm seeing something I can't explain. I have an activerecord object
called a Gift which inherits from ActiveRecord::Base like a typical
rails model (which it is).

However, if I run Gift.find_by_sql I'm getting objects which, when I
call .class say "Gift" but when I call .instance_of?(Gift) one or two
of the 16 rows I get back say "true" and the rest say "false". This
leads me to believe that I'm getting back gift classes and not
instances of gift objects.

How is something like this possible? This problem has been making me
miss statically-typed languages :(

Any advice would be much appreciated.

Thanks,
Tom
 
M

mojombo

Tom said:
However, if I run Gift.find_by_sql I'm getting objects which, when I
call .class say "Gift" but when I call .instance_of?(Gift) one or two
of the 16 rows I get back say "true" and the rest say "false". This
leads me to believe that I'm getting back gift classes and not
instances of gift objects.

How is something like this possible? This problem has been making me
miss statically-typed languages :(

That's curious. If they were Classes being returned, calling #class on
them would return Class.

String.class
=> Class

Try outputting the return from #inspect on each of the objects
returned. That should give you some insight into what they are and what
they contain.

Gift.find_by_sql(...).each do |gift|
puts gift.inspect
end

Tom Werner
 
T

Tom

That's curious. If they were Classes being returned, calling #class on
them would return Class.

I'm pretty sure this was all due to me trying to operate on
activerecord via multiple threads, so I dropped that approach. I do
appreciate the feedback through.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top