#580 suggestion
David Smalley

Sorting on join associations

Reported by David Smalley | September 30th, 2008 @ 12:38 PM

In a project I want to be able to do this....

class Ticket

    has n, :ticket_statuses, :order => [:created_at.desc]
    has n, :statuses, :through => :ticket_statuses


If I call 'ticket.ticket_statuses' I get the following query:

SELECT "ticket_id", "status_id", "created_at" FROM "ticket_statuses" WHERE ("ticket_id" IN ('8D5T-DWUNUG')) ORDER BY "created_at" DESC

But if I call 'ticket.statuses' I get

SELECT "statuses"."id", "statuses"."name", "statuses"."object_class" FROM "statuses" INNER JOIN "ticket_statuses" ON ("statuses"."id" = "ticket_statuses"."status_id") WHERE ("ticket_statuses"."ticket_id" = '8D5T-DWUNUG') GROUP BY "statuses"."id", "statuses"."name", "statuses"."object_class" ORDER BY "statuses"."id"

I then tried to specify the has n, :statuses as:

has n, :statuses, :through => :ticket_statuses, :order => ["ticket_statuses.created_at DESC"] and has n, :statuses, :through => :ticket_statuses, :order => ["ticket_statuses.created_at.desc"]

But both of these fail with

ArgumentError: Unknown property 'ticket_statuses.created_at.desc'

Is it a bug that I cannot apply an order to a join table in this way, or a feature?

Comments and changes to this ticket

  • David Smalley

    David Smalley September 30th, 2008 @ 12:39 PM

    Formatting seems to be broken, hopefully it still all makes sense.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 8th, 2009 @ 05:09 AM

    • State changed from “new” to “accepted”
    • Assigned user changed from “Sam Smoot” to “Dan Kubb (dkubb)”
  • Justin Smestad

    Justin Smestad February 23rd, 2009 @ 04:25 PM

    This is still broken on dm-next. I am getting the same error using his script.

    /tmp/test-bug > ruby bug.rb [] Included HasAddress into Person [] Included Model into Person [] Included HasEmail into Person [] Included Model into Person [!] Notice how the 'address' property is not present


  • Justin Smestad
  • Kabari

    Kabari March 8th, 2009 @ 09:37 PM

    note This error also happens when adding an attribute to the association ie ":property.not => nil"

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 16th, 2009 @ 03:21 AM

    • Milestone set to 0.10.0
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) July 23rd, 2009 @ 11:46 AM

    • Milestone changed from 0.10.0 to 0.10.1
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) October 4th, 2009 @ 09:33 PM

    • Milestone changed from 0.10.1 to 0.10.2

    [project:id#20609 not-tagged:"0.10.0" not-tagged:"0.10.1" milestone:id#51895 bulk edit command]

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) October 6th, 2009 @ 11:41 AM

    • Milestone cleared.
    • State changed from “accepted” to “suggestion”
    • Assigned user cleared.

    Currently we do not know if this will be possible outside of RDBMS, so I am marking this as a "suggestion" to revisit later.

    With an RDBMS this is trivially simple, you just reference the intermediaries' columns in the ORDER BY, and you get the results back sorted by the intermediary.

    With a non-RDBMS, which DataMapper also targets, we don't yet know how m:m relationships will work. It is likely that each intermediary will be fetched, and then joined in-memory with the next relationship in the chain. This will be difficult enough to get right without also throwing intermediary sorting into the mix.

    I think this probably will be accepted at some point, it's just that we've got a number of things far more important to resolve first, from self referential m:m, to m:m with non-RDBMS adapters.

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 »