Let''s say I had Products and Stores. A Product can be in many Stores, and a Store can have many Products. I want a page that lists all the Products by name, and the number of Stores that have that Product, like so: Product # of Stores ---------- -------------- Apple 25 Banana 33 Cat 3 By default it should be listed alphabetically. In my controller, I can do a @products = Product.find_all nil, "name ASC", and in my view print out @product.name and @product.stores_count to get the number of stores. But what if I want to make the headers (Product, # of Stores) links that sort the results on that column? How would I order by the # of Stores column? -Myles
I''d write an AJAX method to reorder the table in Javascript on the client. Then I''d submit the code so other friendly railers could use it! On 5/31/05, Myles Grant <myles-mP7i/ER/+SK8rjiVs5Nzzw@public.gmane.org> wrote:> Let''s say I had Products and Stores. A Product can be in many Stores, > and a Store can have many Products. I want a page that lists all the > Products by name, and the number of Stores that have that Product, like so: > > Product # of Stores > ---------- -------------- > Apple 25 > Banana 33 > Cat 3 > > By default it should be listed alphabetically. In my controller, I can > do a @products = Product.find_all nil, "name ASC", and in my view print > out @product.name and @product.stores_count to get the number of stores. > > But what if I want to make the headers (Product, # of Stores) links that > sort the results on that column? How would I order by the # of Stores > column? > > -Myles > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Barry Walker wrote:>I''d write an AJAX method to reorder the table in Javascript on the client. > >Then I''d submit the code so other friendly railers could use it! > >But what if the table''s long enough that I want it spread over multiple pages? Certainly there''s a way to do this in Rails -- even if I have to use find_by_sql and do the GROUP BY and ORDER BY by hand. -Myles
On Wednesday, June 1, 2005, 9:47:03 AM, Myles wrote:> Barry Walker wrote:>>I''d write an AJAX method to reorder the table in Javascript on the client. >> >>Then I''d submit the code so other friendly railers could use it! >> >> > But what if the table''s long enough that I want it spread over multiple > pages? Certainly there''s a way to do this in Rails -- even if I have to > use find_by_sql and do the GROUP BY and ORDER BY by hand.Is sorting in Ruby acceptable alternative to sorting in SQL? If you have an array of objects ("records") with methods #products and #nstores: records = records.sort_by { |r| r.nstores } That''s what I always do: it feels better (to me) than manipulating SQL queries from Ruby. Gavin
Sorting in SQL will be much faster. If speed isn''t important or the sets aren''t big, sorting in Ruby is probably easier.> Is sorting in Ruby acceptable alternative to sorting in SQL? > > If you have an array of objects ("records") with methods #products and > #nstores: > > records = records.sort_by { |r| r.nstores } > > That''s what I always do: it feels better (to me) than manipulating SQL > queries from Ruby. > > Gavin > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >