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.
B5a793b425c4db1f45c3cdfec5697047?d=identicon&s=25 scott (Guest)
on 2006-06-08 17: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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-06-08 17: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
B5a793b425c4db1f45c3cdfec5697047?d=identicon&s=25 scott (Guest)
on 2006-06-08 17: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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-06-08 17: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
B5a793b425c4db1f45c3cdfec5697047?d=identicon&s=25 scott (Guest)
on 2006-06-08 17:54
Craig White 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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-06-08 18: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
C7563e192f49bf16ffcb126c30f7cb2b?d=identicon&s=25 anonymous coward (Guest)
on 2006-06-08 18:05
maybe orginisationes? (es)
64645a3465d46edd978207de74793023?d=identicon&s=25 anonymous coward (Guest)
on 2006-06-08 18:06
maybe orginisationes? (es)
B5a793b425c4db1f45c3cdfec5697047?d=identicon&s=25 scott (Guest)
on 2006-06-08 18: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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-06-09 00: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.