how to 'delete' an object from an association collection in-memory only

Hi,

My Rails app offers opportunities to try out models on a 'what-if'
basis, where dependent associations can be modified to obtain
alternative results for the parent. Is there a way to delete/destroy
the member of a collection so that I can play around with the
accordingly modified parent in memory, without actually committing the
destruction of the collection member to the data store? Based on my
read of the docs, there is no 'delete' counterpart to
'collection.build' for association members.

Thanks for any comment,

Lille

If I understand you correctly you only need to remove the
association's array element. That will not delete the underlying DB
record if there is one.

Lille wrote in post #965224:

Hi,

My Rails app offers opportunities to try out models on a 'what-if'
basis, where dependent associations can be modified to obtain
alternative results for the parent. Is there a way to delete/destroy
the member of a collection so that I can play around with the
accordingly modified parent in memory, without actually committing the
destruction of the collection member to the data store? Based on my
read of the docs, there is no 'delete' counterpart to
'collection.build' for association members.

If it were my app, I think I'd do this differently: start a transaction
for the "what if" stuff, then either roll it back or commit it when
done.

Thanks for any comment,

Lille

Best,

Hi,

I'm not sure why you are worried about touching the data store.
collection.build doesn't save the new object so there is nothing in
the db that could be deleted.
If all you want to do is revert collection.build just remove that
element of the association array (as pepe pointed out).

comment = @post.comments.build
@post.comments.delete(comment)

or

@post.comments.build
@post.comments.delete(@post.comments.last)

Hope this helps!

Simon