[CDBI] How to sort "has_many" relation after I've declared it?

David McMath mcdave at stanford.edu
Mon Aug 20 21:45:08 BST 2012

Dear List,

In the documentation, the example is given of

	Music::CD->has_many( tracks => 'Music::Track', { order_by => 
'playorder' }) ;

so if I do

	my @t = $cd->tracks

then @t is ordered by "playorder".

If one had been short-sighted and instead just declared

	Music::CD->has_many( tracks => 'Music::Track' ) ;

are there any magical arguments (or something) one could pass to 
$cd->tracks() to sort the results?  I want to do the sorting on the 
database side (which could well be a bad idea, but that's an empirical 

In my particular case (which is basically the same as the Music::CD 
case, so I don't bother listing it), my naive attempt was to just ask:

	my (@s) = $d->schools( { order_by => 'name' } ) ;

but got the message

order_by is not a column of School at 
/usr/lib/perl5/site_perl/5.8.8/Class/DBI/Search/Basic.pm line 115.

The implementation of HasMany seems to have some logic for adding an 
order_by clause to the search, but it only seems to draw that from 

Thansk for any advice,


More information about the ClassDBI mailing list