i have an authors table who has_and_belongs_to_many :books.
In my authors view i can say
@authors.each do |a|
a.books.each do |b|
is there a way to sort (a.books.each) in this view ? I tried
a.books.sort.each but it didnt work.
thanks
adam
Assuming you want to sort by title you need to do something like:
a.books.sort {|x,y|
x.title <=> y.title }.each do |b|
.....
end
Jeremy
Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> writes:
> i have an authors table who has_and_belongs_to_many :books.
>
> In my authors view i can say
>
> @authors.each do |a|
>
> a.books.each do |b|
>
> is there a way to sort (a.books.each) in this view ? I tried
> a.books.sort.each but it didnt work.
>
> thanks
> adam
worked like a charm, thank you very much. adam Jeremy Hughes wrote:> Assuming you want to sort by title you need to do something like: > > a.books.sort {|x,y| > x.title <=> y.title }.each do |b| > ..... > end > > Jeremy > > Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> writes: > > >>i have an authors table who has_and_belongs_to_many :books. >> >>In my authors view i can say >> >> @authors.each do |a| >> >> a.books.each do |b| >> >> is there a way to sort (a.books.each) in this view ? I tried >> a.books.sort.each but it didnt work. >> >>thanks >>adam > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
You can make ActiveRecord do this nicely at the database level:
a.books.find(:all, :order => ''title ASC'').each do ...
or, even better, you can specify the ordering in your model:
class Author < ActiveRecord::Base
has_and_belongs_to_many :books, :order => ''title ASC''
end
which then lets you do:
@authors = Author.find(:all, :include => :books)
which gets each Author along with all his or her Books, in the right
order, and in a single query.
Pete Yandell.
On 08/11/2005, at 6:55 AM, Adam Denenberg wrote:
> i have an authors table who has_and_belongs_to_many :books.
>
> In my authors view i can say
>
> @authors.each do |a|
>
> a.books.each do |b|
>
> is there a way to sort (a.books.each) in this view ? I tried
> a.books.sort.each but it didnt work.
>
> thanks
> adam
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
Pete''s solution is much better :-) Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> writes:> worked like a charm, thank you very much. > > adam > > Jeremy Hughes wrote: >> Assuming you want to sort by title you need to do something like: >> a.books.sort {|x,y| >> x.title <=> y.title }.each do |b| >> ..... >> end >> Jeremy >> Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> writes: >> >>>i have an authors table who has_and_belongs_to_many :books. >>> >>>In my authors view i can say >>> >>> @authors.each do |a| >>> >>> a.books.each do |b| >>> >>> is there a way to sort (a.books.each) in this view ? I tried >>> a.books.sort.each but it didnt work. >>> >>>thanks >>>adam >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails