Sorting through relationships

Hi,

I’m having a rather annoying problem which can only be explained with
an example, so here we go.

I have a Person class which has_many :assets. An Asset has_one :game.
I would like every Person’s asset to be sorted by its game’s title,
similar to this:

has_many :assets, :order => 'game.title'

That doesn’t seem to work. A possible solution is to fall back to
SQL. However, I’m wondering whether there is a ‘native’, no-SQL
ActiveRecord way to do this.

Regards,

Denis D.


mail: [email protected]
web: http://stoneship.org/

Hi Denis,

Am Sonntag, den 06.08.2006, 22:45 +0200 schrieb Denis D.:

I have a Person class which has_many :assets. An Asset has_one :game.
I would like every Person’s asset to be sorted by its game’s title,
similar to this:

has_many :assets, :order => ‘game.title’

That doesn’t seem to work. A possible solution is to fall back to
SQL. However, I’m wondering whether there is a ‘native’, no-SQL
ActiveRecord way to do this.

I solved a similar problem, by setting the :include option to the
referenced table. Though it was in the “find”-method, has_many should
work the same way:

has_many :assets, :include => :game, :order => ‘game.title’

-marek


marek kralewski
tuxwerk ohg
baslerstr 13
d-79100 freiburg
fon +49 (0)761 7043067
fax +49 (0)761 7043066
mobil +49 (0)171 4644052
http://www.tuxwerk.de/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs