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

Discussion in 'Ruby' started by Tom, May 15, 2006.

  1. Tom

    Tom Guest

    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
     
    Tom, May 15, 2006
    #1
    1. Advertising

  2. Tom

    Guest

    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
     
    , May 15, 2006
    #2
    1. Advertising

  3. Tom

    Tom Guest

    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.
     
    Tom, Jun 4, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Sajdera

    ListBox Control Not Returing Selected Values

    Matthew Sajdera, Apr 12, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    174
    Matthew Sajdera
    Apr 12, 2004
  2. Isa Janfada

    Webservice returing a zip file

    Isa Janfada, Mar 18, 2007, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    458
    Guest
    Mar 19, 2007
  3. felix sheng

    find_by_sql and date_format

    felix sheng, Mar 26, 2007, in forum: Ruby
    Replies:
    1
    Views:
    217
    Alex LeDonne
    Mar 26, 2007
  4. Darin Ginther
    Replies:
    7
    Views:
    137
    Darin Ginther
    Dec 31, 2008
  5. Jorge alejandro Mendoza torres

    Can I do a query SQL with various tables using find_by_sql?

    Jorge alejandro Mendoza torres, Mar 5, 2010, in forum: Ruby
    Replies:
    1
    Views:
    136
    Brian Candler
    Mar 5, 2010
Loading...

Share This Page