AnApprentice
AnApprentice

Reputation: 110960

Rails, destroy versus delete?

Why does this work:

@poll_votes = PollVote.where(:user_id => self.user_id, :poll_id => self.poll_id).all

@poll_votes.each do |p|
  p.destroy
end

But this does not?

@poll_votes = PollVote.where(:user_id => self.user_id, :poll_id => self.poll_id).destroy

Upvotes: 5

Views: 6172

Answers (3)

moc
moc

Reputation: 184

This should work: PollVote.destroy_all(:user_id => self.user_id, :poll_id => self.poll_id)

Upvotes: 10

Fred
Fred

Reputation: 8602

The where method returns an enumerable collection of activerecord objects meeting the selection criteria. Calling the destroy method on that collection is different than calling the destroy method on a single activerecord object.

Upvotes: 15

Arun Kumar Arjunan
Arun Kumar Arjunan

Reputation: 6857

'where' is a named scope. You are calling a destroy method on a named-scope collection. Try destroy_all

Upvotes: 1

Related Questions