#465 ✓resolved
Daniel Neighman (hassox)

finder option :fields broken

Reported by Daniel Neighman (hassox) | July 17th, 2008 @ 12:25 PM

When using the :fields finder option DM issues two calls to the db. The first one for the fields, and another to load the missing fields.

Here's an example:

>> Group.all(:fields => [:name])

Thu, 17 Jul 2008 17:19:41 GMT ~ debug ~ SELECT "name" FROM "groups" ORDER BY "name"

Thu, 17 Jul 2008 17:19:41 GMT ~ debug ~ SELECT "value", "name" FROM "groups" WHERE "name" IN ('blah') ORDER BY "name"

It doesn't matter which model uses the :fields option, they all do it since it seems to be implemented in the data_objects_adapter.

The error trace seems to look something like this for .all

data_objects_adapter.rb#48

while(reader.next!)

collection.load(reader.values)

end

collection.rb#22

def load(values)

add(model.load(values, query))

end

model.rb#297

if resource = identity_map.get(key_values)

return resource unless query.reload?

else

This additional get seems to be loading all the other fields. :(

The line numbers are relevant for commit 66cd29bb78dda5eb0500a2e523ceff8334d06594

Comments and changes to this ticket

  • Daniel Neighman (hassox)
  • Bernerd Schaefer

    Bernerd Schaefer July 22nd, 2008 @ 12:10 PM

    • State changed from “new” to “resolved”

    Actually, :fields seems to work correctly. The extra query comes from calling inspect (or pp), so you'd only see it in IRB.

    You can run the attached file to see:

    ruby query_from_inspect.rb

    inspect=true query_from_inspect.rb

  • Daniel Neighman (hassox)

    Daniel Neighman (hassox) July 22nd, 2008 @ 07:11 PM

    Bernerd, I was not calling inspect (I'm aware of that one as an issue for brining down all fields)

    You can see above what I called.

    >> Group.all(:fields => [:name])

    Thu, 17 Jul 2008 17:19:41 GMT ~ debug ~ SELECT "name" FROM "groups" ORDER BY "name"

    Thu, 17 Jul 2008 17:19:41 GMT ~ debug ~ SELECT "value", "name" FROM "groups" WHERE "name" IN ('blah') ORDER BY "name"

    There is no inspect, I just set the logger to debug mode. Unless the logger is calling inspect on the model somewhere I didn't see.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) July 22nd, 2008 @ 09:26 PM

    In IRB the Object#inspect() method is called on whatever value is returned from the last statement you typed in. As a test try something like this:

      Group.all(:fields => [:name]); true
    

    Since the "true" is the return value from the last statement, inspect will be called on it instead of the Collection.

    I spent a few hours chasing a bug report with similar IRB issues only to realize it was due to how IRB calls inspect automatically. If you try the above code and two SQL queries are still issued, please reopen this ticket (or ask for it to be reopened).

  • Daniel Neighman (hassox)

    Daniel Neighman (hassox) July 22nd, 2008 @ 10:49 PM

    Gotcha. Thanx for you're help on this one guys. That's got it sorted. :)

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

Attachments

Pages