Hi all, if I have a has_many and belongs_to relations like this category has many articles article belongs to category is there a way I can specify the order of articles when pulling them up with @some_category.articles ? Many thanks in advance! -- Nicky
Nickolay Kolev wrote:> Hi all, > > if I have a has_many and belongs_to relations like this > > category has many articles > article belongs to category > > is there a way I can specify the order of articles when pulling them up > with > > @some_category.articlesin the category model, add an order condition to the association: has_many :articles, :order => ''name ASC''> ? > > Many thanks in advance! > > -- Nicky > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> in the category model, add an order condition to the association: > has_many :articles, :order => ''name ASC''Thanks! Is is also possible to specify it in the view, so I can make ''order by'' links (order alphabetically, by some inventory number, date entered and so on...)? -- Nicky
* Nickolay Kolev (nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org) [050306 03:21]:> Is is also possible to specify it in the view, so I can make ''order by'' > links (order alphabetically, by some inventory number, date entered and > so on...)?I tend to just use Ruby''s sort_by in the view, because my views expect to have all the necessary data already. I often prepare the data in the controller (often with a before_* filter), but sometimes I do it straight in the view with sort, or relegate the sort_by-ing to a helper function. Rick -- http://www.rickbradley.com MUPRN: 825 | Thursday''s hearing." (San random email haiku | Francisco Examiner, | Friday, June >page B-4).
Nickolay Kolev wrote:>> in the category model, add an order condition to the association: >> has_many :articles, :order => ''name ASC'' > > > Thanks! > > Is is also possible to specify it in the view, so I can make ''order by'' > links (order alphabetically, by some inventory number, date entered and > so on...)?I havn''t tested it but you should be able to say: @articles = @some_category.articles.find_all(nil, ''name ASC'') by passing nil for the conditions parameter of find_all() you retreive all associated records, and the second parameter lets you specify the sort order. dylan