简体   繁体   中英

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

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.

这应该工作:PollVote.destroy_all(:user_id => self.user_id,:poll_id => self.poll_id)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM