Hi, What is the best way to delete all members of a model? For example I have a tasks model and want to put a link in that will delete all tasks at once. Something like, @tasks = Task.all @tasks.each { |task| task.destroy } Do I need another action for this, and what should the link to it be? cheers, DAZ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Fri, Dec 19, 2008 at 12:20 PM, DAZ <daz4126-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > What is the best way to delete all members of a model? > > For example I have a tasks model and want to put a link in that will > delete all tasks at once. Something like, > > @tasks = Task.all > @tasks.each { |task| task.destroy }If you need to allow callbacks to run or dependent associations to also be destroyed, you can use Task.destroy_all. If you don''t need those things, Task.delete_all will be much faster.> Do I need another action for this, and what should the link to it be?You could add another route. For example, map.resources :tasks, :collection => { :destroy_all => :delete } In TasksController, you''d add something like def destroy_all Task.destroy_all # redirect, render some RJS, etc. end In a view, you could link_to "Delete all", destroy_all_tasks_path You might want to put a confirmation on the link_to. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
cheers Craig, those instructions were really clear and worked perfectly. Thanks again, DAZ On Dec 19, 5:33 pm, "Craig Demyanovich" <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Dec 19, 2008 at 12:20 PM, DAZ <daz4...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > What is the best way to delete all members of a model? > > > For example I have a tasks model and want to put a link in that will > > delete all tasks at once. Something like, > > > @tasks = Task.all > > @tasks.each { |task| task.destroy } > > If you need to allow callbacks to run or dependent associations to also be > destroyed, you can use Task.destroy_all. If you don''t need those things, > Task.delete_all will be much faster. > > > Do I need another action for this, and what should the link to it be? > > You could add another route. For example, > > map.resources :tasks, :collection => { :destroy_all => :delete } > > In TasksController, you''d add something like > > def destroy_all > Task.destroy_all > # redirect, render some RJS, etc. > end > > In a view, you could > > link_to "Delete all", destroy_all_tasks_path > > You might want to put a confirmation on the link_to. > > Regards, > Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''d now like to take this a step further and allow users to select which tasks they want to delete (using check boxes probably) then select ''delete all checked tasks''. Does anybody have any pointers on how to go about this and how the form would look? I''m guessing that I would need to create a @selected_tasks array somehow from what was submitted and then call delete_all on that? cheers, DAZ On Dec 19, 5:20 pm, DAZ <daz4...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > What is the best way to delete all members of a model? > > For example I have a tasks model and want to put a link in that will > delete all tasks at once. Something like, > > @tasks = Task.all > @tasks.each { |task| task.destroy } > > Do I need another action for this, and what should the link to it be? > > cheers, > > DAZ--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---