Sep 26, 2007So, how many times have you had the urge to write:
Lots? Me too. So, I think it's worth clearing the air on our good friend association proxy. He really, really wants you to believe he's an array. He'll even tell you he is.
But, don't believe him, because a lot of his array functionality is useless to us. Your favourite rubyisms (reject!, select!, etc) will modify the collection, but will not modify the associations. This test passes:
Instead, of course, you have to use the delete method.
def test_association_proxy_really_looks_like_an_array_but_it_isnt assert Post.find(1).comments.is_a?(Array) assert Post.find(1).comments.select(&:spam?).length > 0 post = Post.find(1) post.comments.reject!(&:spam?) post.save! assert_equal , post.comments.select(&:spam?) assert Post.find(1).comments.select(&:spam?).length > 0 end
Association proxy is tricky.