mrlongleg1962-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Feb-04 23:33 UTC
delete_if does not work on associations
I tried to user delete_if on an association: class Group < ActiveRecord::Base has_many :memberships, :dependent => :destroy has_many :users, :through => :memberships ... end This is the controller method, trying to use delete_if. Although some elements are removed (s2 < s1), the save method does not update the association. def intersect_or_remove_group other_group = Group.find(params[:group_id]) s1 = @group.users.size case params[:submit] when "Intersect" @group.users.delete_if { |u| !other_group.users.include?(u) } when "Remove" @group.users.delete_if { |u| other_group.users.include?(u) } end s2 = @group.users.size if s2 < s1 @group.save end redirect_to @group end If I rewrite it like this, it works just fine: def intersect_or_remove_group other_group = Group.find(params[:group_id]) s1 = @group.users.size to_be_deleted = Array.new case params[:submit] when "Intersect" @group.users.each { |u| to_be_deleted << u if ! other_group.users.include?(u) } when "Remove" @group.users.each { |u| to_be_deleted << u if other_group.users.include?(u) } end to_be_deleted.each { |u| @group.users.delete(u) } s2 = @group.users.size if s2 < s1 @group.save end redirect_to @group end Is this a bug or a feature? Thanks for any help in advance Regards Alexander --~--~---------~--~----~------------~-------~--~----~ 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 Feb 4, 11:33 pm, "mrlongleg1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <mrlongleg1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I tried to user delete_if on an association: > > class Group < ActiveRecord::Base > has_many :memberships, :dependent => :destroy > has_many :users, :through => :memberships > > ... > end > > This is the controller method, trying to use delete_if. Although some > elements are removed (s2 < s1), the save method does not update the > association.Looks like delete_if just acts on the target array, not the association itself (as a slight typing saver you can pass an array to delete. Fred> > def intersect_or_remove_group > other_group = Group.find(params[:group_id]) > s1 = @group.users.size > case params[:submit] > when "Intersect" > @group.users.delete_if { |u| !other_group.users.include?(u) } > when "Remove" > @group.users.delete_if { |u| other_group.users.include?(u) } > end > s2 = @group.users.size > if s2 < s1 > @group.save > end > redirect_to @group > end > > If I rewrite it like this, it works just fine: > > def intersect_or_remove_group > other_group = Group.find(params[:group_id]) > s1 = @group.users.size > to_be_deleted = Array.new > case params[:submit] > when "Intersect" > @group.users.each { |u| to_be_deleted << u if ! > other_group.users.include?(u) } > when "Remove" > @group.users.each { |u| to_be_deleted << u if > other_group.users.include?(u) } > end > to_be_deleted.each { |u| @group.users.delete(u) } > s2 = @group.users.size > if s2 < s1 > @group.save > end > redirect_to @group > end > > Is this a bug or a feature? > > Thanks for any help in advance > > Regards > > Alexander--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
mrlongleg1962-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Feb-05 18:56 UTC
Re: delete_if does not work on associations
Thanks for the hint. Probably this is a bug, because the association is not updated correctly. Unfortunately I am not advanced enough in rails to be able to provide a patch for it. Alexander alias MrLongleg On 5 Feb., 04:38, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 4, 11:33 pm, "mrlongleg1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" > > <mrlongleg1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I tried to user delete_if on an association: > > > class Group < ActiveRecord::Base > > has_many :memberships, :dependent => :destroy > > has_many :users, :through => :memberships > > > ... > > end > > > This is the controller method, trying to use delete_if. Although some > > elements are removed (s2 < s1), the save method does not update the > > association. > > Looks like delete_if just acts on the target array, not the > association itself (as a slight typing saver you can pass an array to > delete. > > Fred > > > > > def intersect_or_remove_group > > other_group = Group.find(params[:group_id]) > > s1 = @group.users.size > > case params[:submit] > > when "Intersect" > > @group.users.delete_if { |u| !other_group.users.include?(u) } > > when "Remove" > > @group.users.delete_if { |u| other_group.users.include?(u) } > > end > > s2 = @group.users.size > > if s2 < s1 > > @group.save > > end > > redirect_to @group > > end > > > If I rewrite it like this, it works just fine: > > > def intersect_or_remove_group > > other_group = Group.find(params[:group_id]) > > s1 = @group.users.size > > to_be_deleted = Array.new > > case params[:submit] > > when "Intersect" > > @group.users.each { |u| to_be_deleted << u if ! > > other_group.users.include?(u) } > > when "Remove" > > @group.users.each { |u| to_be_deleted << u if > > other_group.users.include?(u) } > > end > > to_be_deleted.each { |u| @group.users.delete(u) } > > s2 = @group.users.size > > if s2 < s1 > > @group.save > > end > > redirect_to @group > > end > > > Is this a bug or a feature? > > > Thanks for any help in advance > > > Regards > > > Alexander--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- @article.article_groups.delete_if... Dosn't work!
- shouldn''t this work? - session[:array_of_objects].delete_if {|x| x.id == params[:id]}
- delete_if doesn''t work for has_and_belongs_to_many
- @article.article_groups.delete_if... Dosn''t work!
- Rails 4 deprecation of in-place edit methods for collection associations