Ruby on Rails Monday, September 23, 2013



On Tuesday, 17 September 2013 00:43:15 UTC-5, George Georgiev wrote:
Hi everyone!

For those of you who are not noticed the in-place edit method for collection associations are broken in the current rails release - 4.0.0,

In short: it looks like things like has_many.reject!{}, has_many.select!{}, has_many.delete_if{}, has_many.pop etc. will not work as of Rails 4.2.
I tried to get some answers about the motivation of this decision here https://github.com/rails/rails/pull/12236 but i failed.

So to the question can someone please explain WHY is this happening?
Where is the "confusion" in the examples above?

 Another related ticket: https://github.com/rails/rails/issues/12140

The problem is that using (for instance) delete_if on a collection removes things from the in-memory version but doesn't persist the changes. It *can't* persist the changes, since the records have been removed from the association's list of records. This is EXACTLY the "confusion" referenced in the tickets.

One of your examples in 12236 highlights this issue: these two code snippets (in Rails 3 or 4) do not do the same thing:

post.comments.select! { |comment| comment.not_spam? } 
# => filters the array in-memory but does not unlink objects

vs

comments = post.comments.to_a
comments.select! { |comment| comment.not_spam? }
post.comments = comments 
# => unlinks comments that aren't in the filtered array

--Matt Jones

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/18f8ba4e-29ec-4c04-863f-1ac857ef4e02%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment