I am after a nice way to disable (not destroy, delete nor nullify) all associated records. I''m a hoarder, I don''t want to get rid of anything but I want to stop records being visible. I have a disabled boolean in my tables and if I disable the top record in the association (say the association is 5-6 levels deep), I would like to cleanly disable its children. I''ve looked in the source to see how AR collects all the records but couldn''t easily track it back. The way I''m thinking is to extend .... has_many :products do def disable products = all(:conditions => '''', :include => associations) products.each do ## but here it gets messy end end end Does anyone have a nicer solution to this? Thank you -ants -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sent from my iPhone On Apr 6, 2011, at 5:05 AM, Ants Pants <antsmailinglist-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am after a nice way to disable (not destroy, delete nor nullify) all associated records. I''m a hoarder, I don''t want to get rid of anything but I want to stop records being visible. > > I have a disabled boolean in my tables and if I disable the top record in the association (say the association is 5-6 levels deep), I would like to cleanly disable its children. > > I''ve looked in the source to see how AR collects all the records but couldn''t easily track it back. > > The way I''m thinking is to extend .... > > has_many :products do > def disable > products = all(:conditions => '''', :include => associations) > products.each do > ## but here it gets messy > end > > end > end > > Does anyone have a nicer solution to this?I would set the has_many dependency to delete/destroy then in the model products redefine what the delete/destroy method does. def delete #set disable Boolean end This not only works for your cascading dependencies but also if you call the delete method from an object of products. B. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yes, that''s nice. Thanks for that. For some reason, I didn''t think the has_many would call my delete but I guess it does. Again, thanks. -ants On 6 April 2011 14:40, Bryan Crossland <bacrossland-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sent from my iPhone > > On Apr 6, 2011, at 5:05 AM, Ants Pants <antsmailinglist-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am after a nice way to disable (not destroy, delete nor nullify) all > associated records. I''m a hoarder, I don''t want to get rid of anything but I > want to stop records being visible. > > > > I have a disabled boolean in my tables and if I disable the top record in > the association (say the association is 5-6 levels deep), I would like to > cleanly disable its children. > > > > I''ve looked in the source to see how AR collects all the records but > couldn''t easily track it back. > > > > The way I''m thinking is to extend .... > > > > has_many :products do > > def disable > > products = all(:conditions => '''', :include => associations) > > products.each do > > ## but here it gets messy > > end > > > > end > > end > > > > Does anyone have a nicer solution to this? > > I would set the has_many dependency to delete/destroy then in the model > products redefine what the delete/destroy method does. > > def delete > #set disable Boolean > end > > This not only works for your cascading dependencies but also if you call > the delete method from an object of products. > > B. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.