Order by attributes of association.

17 views
Skip to first unread message

Cyril Mougel

unread,
Feb 26, 2009, 10:47:04 AM2/26/09
to datam...@googlegroups.com
Hi,

I have one case where I don't know how made.

I have some tickets with one user by ticket. My objects are :

class Ticket
property :id, Serial
property :name, String
belongs_to :user
end

class User
property :id, Serial
property :name, String

has n, :tickets
end

If I want search a ticket with user name, i can do :

Ticket.all( 'user.name' => 'foo')

But If I want ordering my ticket by user.name I can't.

Ticket.all :order => 'user.name'

There are no way to do that ? because my user.id is not same order that
name.

Thanks for you help

--
Cyril Mougel
http://blog.shingara.fr

web...@yahoo.co.uk

unread,
Feb 26, 2009, 11:56:38 AM2/26/09
to DataMapper
Could try this..

Ticket.all(:order => [:title.asc])

Had a bit of trouble with this myself..

Cyril Mougel

unread,
Feb 26, 2009, 12:03:00 PM2/26/09
to datam...@googlegroups.com
web...@yahoo.co.uk wrote:
> Could try this..
>
> Ticket.all(:order => [:title.asc])
>
> Had a bit of trouble with this myself..
>
this order works only to order by a propertie of Ticket. Not to association.

RLivsey

unread,
Apr 1, 2009, 6:31:54 AM4/1/09
to DataMapper
On Feb 26, 7:03 pm, Cyril Mougel <cyril.mou...@gmail.com> wrote:
> webr...@yahoo.co.uk wrote:
> > Could try this..
>
> > Ticket.all(:order=> [:title.asc])
>
> > Had a bit of trouble with this myself..
>
> thisorderworks only toorderby a propertie of Ticket. Not to association.

I'm running into this problem myself at the moment.

I have the following models:

class Meeting
has n, :minutes
end

class Minute
belongs_to :meeting
end

I'm trying to get all minutes ordered by a property of the meeting
they belong to, so the SQL would be something like:

SELECT minutes.created_at, meetings.title FROM minutes
INNER JOIN meetings ON minutes.meeting_id = meetings.id
ORDER BY meetings.start_at ASC, minutes.created_at ASC

I've tried the following, but get an error for all of them along the
lines of:
ArgumentError: Unknown property 'meeting.start_at.asc'

Minute.all(:order => [:start_at.asc])
Minute.all(:order => ["meeting.start_at.asc"])
Minute.all(:order => ["meeting.start_at asc"])
Minute.all(:order => ["meetings.start_at.asc"])
etc...

Is this possible?

Thanks in advance.

Cyril Mougel

unread,
Apr 1, 2009, 6:38:54 AM4/1/09
to datam...@googlegroups.com

For me not. I have same issue and I check code. but actually nothing is
possible.

When have time, I want try to integrate it. But now I can't :(

Sergei Zimakov

unread,
Apr 10, 2009, 8:55:00 AM4/10/09
to DataMapper
I've found that

Ticket.all(:order => [DataMapper::Query::Direction.new(User.properties
[:name])], :links => [:user])

does exactly what you want from Ticket.all(:order => 'user.name').

On 1 апр, 14:38, Cyril Mougel <cyril.mou...@gmail.com> wrote:
> RLivsey wrote:
> > On Feb 26, 7:03 pm, Cyril Mougel <cyril.mou...@gmail.com> wrote:
>
> >> webr...@yahoo.co.uk wrote:
>
> >>> Could try this..
>
> >>> Ticket.all(:order=> [:title.asc])
>
> >>> Had a bit of trouble with this myself..
>
> >> thisorderworks only toorderby a propertie of Ticket. Not toassociation.
>
> > I'm running into this problem myself at the moment.
>
> > I have the following models:
>
> > class Meeting
> >   has n, :minutes
> > end
>
> > class Minute
> >   belongs_to :meeting
> > end
>
> > I'm trying to get all minutes ordered by a property of the meeting
> > they belong to, so the SQL would be something like:
>
> > SELECT minutes.created_at, meetings.title FROM minutes
> > INNER JOIN meetings ON minutes.meeting_id = meetings.id
> >ORDERBY meetings.start_at ASC, minutes.created_at ASC
>
> > I've tried the following, but get an error for all of them along the
> > lines of:
> > ArgumentError: Unknown property 'meeting.start_at.asc'
>
> > Minute.all(:order=> [:start_at.asc])
> > Minute.all(:order=> ["meeting.start_at.asc"])
> > Minute.all(:order=> ["meeting.start_at asc"])
> > Minute.all(:order=> ["meetings.start_at.asc"])
Reply all
Reply to author
Forward
0 new messages