#194 invalid
Chris Sessions

[patch] has_many :things, :order => 'position' support

Reported by Chris Sessions | March 25th, 2008 @ 07:30 PM

I have a profile model with the following association:

has_many :pics, :order => 'position'

When I request a profile's pics the order is not reflected in the result (I've removed a lot of extraneous fields for clarity):

> Profile[1].pics
SELECT `id`, `login`, [...] FROM `profiles` WHERE `id` = 1
SELECT `id`, [...] FROM `pics` WHERE (`profile_id` IN (1, 1)) ORDER BY position
=> [#<Pic:0x2598574 @new_record=false, @profile_id=1, @position=1, @id=1>, #<Pic:0x259b7ec @new_record=false, @profile_id=1, @position=0, @id=2>]

When I use a database.query call with the exact same SQL the results do come back in the right order.

> database.query("SELECT `id`, [...] FROM `pics` WHERE (`profile_id` IN (1, 1)) ORDER BY position")
=> [#<struct #<Class:0x2579930> id="2", position="0", profile_id="1">, #<struct #<Class:0x2579930> id="1", position="1", profile_id="1">]

I'm using the latest stable 0.3.0 gem. Thanks!

Comments and changes to this ticket

  • Kieran Huggins

    Kieran Huggins March 27th, 2008 @ 08:26 AM

    I just ran into this as well, had to sort the association array manually after.

    What could also be cool is if :order => :anything automagically managed the order on create() and save() as well. Or would that be un-datamappery?

    Cheers!

    Kieran

  • Lucas Carlson

    Lucas Carlson April 14th, 2008 @ 01:46 PM

    This is a bug in DM 0.3. They used a SortedSet data structure to contain the results... but SortedSets manipulate arbitrary sorting. You can fix this with a hack.

    DataMapper::Support::TypedSet.class_eval do

    def initialize(*types)

    @types = types

    @set = []

    end

    end

  • Lucas Carlson

    Lucas Carlson April 14th, 2008 @ 05:02 PM

    Here is a patch to fix 2 problems.

    1) The above issue with ordering

    2) Optimize the query

    SELECT `id`, [...] FROM `pics` WHERE (`profile_id` IN (1, 1)) ORDER BY position

    Notice that (1, 1) shows up. My patch turns this into:

    SELECT `id`, [...] FROM `pics` WHERE (`profile_id` IN (1)) ORDER BY position

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) April 14th, 2008 @ 05:17 PM

    • Milestone cleared.
    • State changed from “new” to “open”
  • Lucas Carlson
  • Lucas Carlson

    Lucas Carlson April 15th, 2008 @ 11:25 AM

    • Title changed from “Is has_many :things, :order => 'position' supported?” to “[patch] has_many :things, :order => 'position' support”
  • Sam Smoot

    Sam Smoot May 8th, 2008 @ 05:15 PM

    • Assigned user set to “Adam French”

    Adam, please apply to DM 0.3.0 and mark as resolved.

  • Adam French

    Adam French May 8th, 2008 @ 05:25 PM

    applying the patch breaks a few specs which have to do with the SortedSet -> Array swap in the patch. Array does not respond to :deleted? in the same way SortedSet does.

    sam: Hack or invalidate?

  • Adam French

    Adam French May 8th, 2008 @ 05:29 PM

    • State changed from “open” to “invalid”

    he said invalidate. If this is still an issue later on, please come back and say so.

    BTW, ya'll should really switch to 0.9 ;-)

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