简体   繁体   English

Rails,销毁与删除?

[英]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. where方法返回符合选择条件的可枚举的activerecord对象集合。 Calling the destroy method on that collection is different than calling the destroy method on a single activerecord object. 在该集合上调用destroy方法与在单个activerecord对象上调用destroy方法不同。

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

'where' is a named scope. 'where'是命名范围。 You are calling a destroy method on a named-scope collection. 您正在命名范围集合上调用destroy方法。 Try destroy_all 尝试destroy_all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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