I need to completely reorder a list. Here''s the structure: Page has_many Sections, and a Section acts_as_list (within the scope of a page). My web tier is AJAX''ing in a list of section id''s in a new ordering (nice browser drag/drop stuff I adopted from taskthis). What is the recommended (or suggested) ways to reorder this list? I created the reorder_sections method below that works, but I''m not satisfied: class Page < ActiveRecord::Base belongs_to :exhibit has_many :sections, :order => :position def reorder_sections(section_ids) sections.each do |section| section.position = section_ids.index(section.id.to_s) section.save # TODO surely a better way to do this so a save is not needed for each section end end end I expected that @page.save would work without having to have the section.save''s in there, but it didn''t. Where is my understanding lacking? Any other improvements in this are most welcome! Thanks, Erik
If you look at the source for acts_as_list, it does custom sql updates instead of using .save. While that''s not the prettiest way to do it, it''s probably the fastest. Tyler On 5/26/05, Erik Hatcher <erik-LIifS8st6VgJvtFkdXX2HpqQE7yCjDx5@public.gmane.org> wrote:> I need to completely reorder a list. Here''s the structure: > > Page has_many Sections, and a Section acts_as_list (within the > scope of a page). > > My web tier is AJAX''ing in a list of section id''s in a new ordering > (nice browser drag/drop stuff I adopted from taskthis). What is the > recommended (or suggested) ways to reorder this list? > > I created the reorder_sections method below that works, but I''m not > satisfied: > > class Page < ActiveRecord::Base > belongs_to :exhibit > has_many :sections, :order => :position > > def reorder_sections(section_ids) > sections.each do |section| > section.position = section_ids.index(section.id.to_s) > section.save # TODO surely a better way to do this so a > save is not needed for each section > end > end > end > > I expected that @page.save would work without having to have the > section.save''s in there, but it didn''t. Where is my understanding > lacking? Any other improvements in this are most welcome! > > Thanks, > Erik > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On May 27, 2005, at 10:03 AM, Tyler Kiley wrote:> If you look at the source for acts_as_list, it does custom sql updates > instead of using .save. While that''s not the prettiest way to do it, > it''s probably the fastest.Tyler - thanks for your reply. I''m not following what you mean though - when would these custom SQL updates occur? I presume only when I call the acts_as_list methods like move_lower and move_higher. I didn''t see where acts_as_list provides anything to completely reorder the list, but only to move things up/down/top/bottom. Could you point me a bit further with this? Thanks, Erik> > Tyler > > On 5/26/05, Erik Hatcher <erik-LIifS8st6VgJvtFkdXX2HpqQE7yCjDx5@public.gmane.org> wrote: > >> I need to completely reorder a list. Here''s the structure: >> >> Page has_many Sections, and a Section acts_as_list (within the >> scope of a page). >> >> My web tier is AJAX''ing in a list of section id''s in a new ordering >> (nice browser drag/drop stuff I adopted from taskthis). What is the >> recommended (or suggested) ways to reorder this list? >> >> I created the reorder_sections method below that works, but I''m not >> satisfied: >> >> class Page < ActiveRecord::Base >> belongs_to :exhibit >> has_many :sections, :order => :position >> >> def reorder_sections(section_ids) >> sections.each do |section| >> section.position = section_ids.index(section.id.to_s) >> section.save # TODO surely a better way to do this so a >> save is not needed for each section >> end >> end >> end >> >> I expected that @page.save would work without having to have the >> section.save''s in there, but it didn''t. Where is my understanding >> lacking? Any other improvements in this are most welcome! >> >> Thanks, >> Erik >> >> _______________________________________________ >> 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 >