Reputation: 110960
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
Reputation: 184
This should work: PollVote.destroy_all(:user_id => self.user_id, :poll_id => self.poll_id)
Upvotes: 10
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
Reputation: 6857
'where' is a named scope. You are calling a destroy method on a named-scope collection. Try destroy_all
Upvotes: 1