Array.delete (object) is deleted from the database?

So, I was trying to just get an array of posts that don't include @post. To my surprise, the code below resulted in @post being removed from the database!

@post = Post.find(2)
@posts = Post.where(:text => "title")
@posts.delete(@post)

      

Why does the array function remove @post from the database? Does Rails extend or overwrite this functionality? I thought that only the .destroy function can delete objects from the database? I guess I'm confused about what exactly the .where function returns and the implications of using @ rather than using @ on variables.

Thanks in advance!

+3


source to share


1 answer


What is stored @posts

after execution is Post.where(:text => 'title')

not an array. This is the type of object ActiveRecord::Relation

.



When you call delete with @post

as a parameter, you are telling Rails to remove that object from the database.

+6


source







All Articles