1

Whenever I run a where clause the query is not executed until I try to access the actual data from the results. Is there anyway I can make it automatically execute.

Example:

delete_list = Rom::Leaderboard.where("pvp_type = ? AND server_id = ?", pvptype, server_id)

delete_list now holds an ActiveRecord object. But I want it to hold the resulting array without having to run something like

delete_list.each ...
4

2 回答 2

1

要急切加载整个查询,您可以使用.all

delete_list = Rom::Leaderboard.where(...).all

这将导致delete_list成为一个数组。

于 2013-10-03T18:51:45.203 回答
0

好吧,如果您只是想立即删除、调用destroy_alldelete_all运行

Rom::Leaderboard.where("pvp_type = ? AND server_id = ?", pvptype, server_id).destroy_all

或者

delete_list = Rom::Leaderboard.where("pvp_type = ? AND server_id = ?", pvptype, server_id)
delete_list.destroy_all

destroy_all至于和的区别delete_all

  • destroy_all:通过调用其destroy方法,关联对象与该对象一起被销毁。这将有效地循环遍历每个对象,并调用destroy它。因此,每个对象至少有 1 个查询。
  • delete_all: 所有关联的对象都会立即销毁,而不调用它们的 :destroy 方法。delete from leader_boards这基本上使用您的 where 子句创建一个查询。因此,无论您有多少对象,它都只运行 1 个查询。但是,不会清除任何关联的对象。
于 2013-10-03T18:50:36.853 回答