Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > find_by_sql returing a class and not an instance of a class?

Reply
Thread Tools

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

 
 
Tom
Guest
Posts: n/a
 
      05-15-2006
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

 
Reply With Quote
 
 
 
 
mojombo@gmail.com
Guest
Posts: n/a
 
      05-15-2006
Tom wrote:
> 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

 
Reply With Quote
 
 
 
 
Tom
Guest
Posts: n/a
 
      06-04-2006
wrote:
> Tom wrote:
> > 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.


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I do a query SQL with various tables using find_by_sql? Jorge alejandro Mendoza torres Ruby 1 03-05-2010 10:35 PM
Creating a class for returing DB results Darin Ginther Ruby 7 12-31-2008 03:43 PM
find_by_sql and date_format felix sheng Ruby 1 03-26-2007 05:50 PM
Webservice returing a zip file Isa Janfada ASP .Net Web Services 1 03-19-2007 08:00 AM
ListBox Control Not Returing Selected Values Matthew Sajdera ASP .Net Web Controls 1 04-12-2004 09:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57