#106 ✓resolved
Jeffrey Gelens

[Request for Comments] Custom queries return wrong types

Reported by Jeffrey Gelens | December 27th, 2007 @ 09:49 AM

Somehow my previous bugreport got lost, so here we go again:

Using MySQL or SQlite3 all returned fields are a string, while some of them should be an integer.

irb(main):006:0> (Service.find_by_sql("select * from services"))[0]
=> #<struct #<Class:0xb7433368> id="1", server_id="1", name="M1-Domain0", status="0", monitor="1", collected="1198550696", kind="5">

Using PostgreSQL everything is ok:

irb(main):003:0> (Service.find_by_sql("select * from services"))[0]
=> #<struct #<Class:0xb7b02aa4> id=1790, name="M1-Domain0", status=0, monitor=true, collected=1198551630, kind=5, server_id=8>

Comments and changes to this ticket

  • Sam Smoot

    Sam Smoot December 27th, 2007 @ 10:09 AM

    • State changed from “new” to “open”

    Hmm... this is a DO issue. Notice we deviate from AR here in that Service.find_by_sql doesn't return Service objects. It returns Structs.

    The idea is that those objects are "unsafe" to save, because we don't know if you selected all the appropriate fields. In fact, we should probably remove find_by_sql altogether. It's just an alias to database.query anyways. So you could do something like this instead if you really wanted them:

      "select * from services"
    ).map { |row| Service.new(row) }

    This brings up another issue though... the values aren't type-casted in the object. So you still wouldn't get type-casted values. (Unless the type-cast issues were resolved in DO.)

    Maybe this should change...

  • Sam Smoot

    Sam Smoot December 27th, 2007 @ 10:21 AM

    • Title changed from “[Bug] Custom queries return wrong types” to “[Request for Comments] Custom queries return wrong types”

    I'd like to get some sort of discussion going on what to do here...

  • Quinn Slack

    Quinn Slack January 2nd, 2008 @ 10:08 PM

    The biggest issue for me with this is that you can't use associations on records retrieved using find_by_sql.

    What if find_by_sql just returned read-only objects of the requested class (instead of struct), with additional getter methods for each column requested in SQL but not present in the model's properties definition? (For example, a `score` column computed by MySQL fulltext search would be accessible as Article#score.)

  • Dan Kubb (dkubb)
  • Sam Smoot

    Sam Smoot April 28th, 2008 @ 05:42 PM

    • State changed from “open” to “resolved”

    This is resolved in 0.9.0.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket