Forum: Ruby on Rails find :order =>

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
scott (Guest)
on 2006-06-08 19:12
Hi,

I have the following find statment

 @client_pages, @clients = paginate :clients, :per_page => 20, :order =>
"organisation_id, surname"

currently im ordering by organisation_id however I need to order by the
field in the organisation table organisations.name

how can this be done?

Thanks
Scott
Craig W. (Guest)
on 2006-06-08 19:31
(Received via mailing list)
On Thu, 2006-06-08 at 17:12 +0200, scott wrote:
> how can this be done?
----
assuming that this is related by something like Class Organisations has
many :clients

@client_pages, @clients = paginate(:clients,
  :include => ['organizations'],
  :order_by => 'organizations.name',
  :per_page => 20)

Craig
scott (Guest)
on 2006-06-08 19:43
yes, the class starts with

class Organisation < ActiveRecord::Base
has_many :clients

When I insert the statment above changing the z to a s (uk,english
spelling), I get the error
Association named 'organisations' was not found; perhaps you misspelled
it?

any suggestions to what ive missed? the table name is "organisations"

Scott
Craig W. (Guest)
on 2006-06-08 19:50
(Received via mailing list)
On Thu, 2006-06-08 at 17:43 +0200, scott wrote:
> any suggestions to what ive missed? the table name is "organisations"
----
does Class Clients
belongs_to :organisation
?

Craig
scott (Guest)
on 2006-06-08 19:54
Craig W. wrote:
> On Thu, 2006-06-08 at 17:43 +0200, scott wrote:
>> any suggestions to what ive missed? the table name is "organisations"
> ----
> does Class Clients
> belongs_to :organisation
> ?
>
> Craig

yes

class Client < ActiveRecord::Base

belongs_to :organisation
Craig W. (Guest)
on 2006-06-08 20:02
(Received via mailing list)
On Thu, 2006-06-08 at 17:54 +0200, scott wrote:
> yes
>
> class Client < ActiveRecord::Base
>
> belongs_to :organisation
----
I'm wondering if it's a pluralization thing...

@client_pages, @clients = paginate(:clients,
  :include => ['organisation'],
  :order_by => 'organisation.name',
  :per_page => 20)

Craig
anonymous coward (Guest)
on 2006-06-08 20:05
maybe orginisationes? (es)
anonymous coward (Guest)
on 2006-06-08 20:06
maybe orginisationes? (es)
scott (Guest)
on 2006-06-08 20:06
nope that didnt work

ive taken a quick look at the api and have tried using join instead of
include, which seems to work.

Not as ncie looking as the idea above as it needs a conditions, but
seems to be working ok for now


@client_pages, @clients = paginate :clients,
                                        :join => ',organisations',
                                        :per_page => 20,
                                        :conditions =>
'clients.organisation_id = organisations.id' ,
                                        :order_by =>
"organisations.name, surname"

Thanks
Craig W. (Guest)
on 2006-06-09 02:05
(Received via mailing list)
On Thu, 2006-06-08 at 18:06 +0200, scott wrote:
>                                         :join => ',organisations',
>                                         :per_page => 20,
>                                         :conditions =>
> 'clients.organisation_id = organisations.id' ,
>                                         :order_by =>
> "organisations.name, surname"
----
the fact that you had to use a join rather than the association means
that there is something wrong with your association as you have created
it.

Craig
This topic is locked and can not be replied to.