Ask Your Question
0

Is it impossible to delete any objects provided by ActiveRecord_Associations_CollectionProxy?

asked 2023-04-06 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-12 08:00:00 +0000

david gravatar image

No, it is not impossible to delete objects provided by ActiveRecordAssociationsCollectionProxy.

You can delete the objects by calling the destroy method on the ActiveRecord_Associations_CollectionProxy object or by calling the destroy_all method on the associated objects.

For example:

# assuming you have a has_many association between User and Post
user = User.first
posts = user.posts # posts is an ActiveRecord_Associations_CollectionProxy object

# deleting a specific object
post = posts.first
post.destroy

# deleting all objects
posts.destroy_all

It's important to note that deleting objects using these methods will also delete any associated objects that depend on them.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-06 11:00:00 +0000

Seen: 11 times

Last updated: Oct 12 '21