delete_all和destroy_all的不同行为

时间:2015-02-10 03:25:28

标签: mysql sql ruby-on-rails rails-activerecord

我有一个关系,我想删除

的所有行

Match.find(10237).inform_requests.near(Bar.first).via_email

[5] pry(main)> Match.find(10237).inform_requests.near(Bar.first).via_email.class
=> InformRequest::ActiveRecord_AssociationRelation

尝试destroy_all生成非常破碎的SQL ..

[6] pry(main)> Match.find(10237).inform_requests.near(Bar.first).via_email.destroy_all
  Match Load (0.4ms)  SELECT  `matches`.* FROM `matches` WHERE `matches`.`id` = 10237 LIMIT 1
  Bar Load (0.2ms)  SELECT  `bars`.* FROM `bars`  ORDER BY `bars`.`id` ASC LIMIT 1
  InformRequest Load (0.2ms)  SELECT `inform_requests`.* FROM `inform_requests` WHERE `inform_requests`.`match_id` = 10237 AND (lat < 1.48591 and lat > 1.0859100000000002) AND (lng < 104.05 and lng > 103.64999999999999) AND (`inform_requests`.`email` IS NOT NULL)
   (0.1ms)  BEGIN
  SQL (0.3ms)  DELETE FROM `inform_requests` WHERE `inform_requests`.`` = NULL
Mysql2::Error: Unknown column 'inform_requests.' in 'where clause': DELETE FROM `inform_requests` WHERE `inform_requests`.`` = NULL
   (0.1ms)  ROLLBACK
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'inform_requests.' in 'where clause': DELETE FROM `inform_requests` WHERE `inform_requests`.`` = NULL

delete_all效果很好

[8] pry(main)> Match.find(10237).inform_requests.near(Bar.first).via_email.delete_all
  Match Load (0.3ms)  SELECT  `matches`.* FROM `matches` WHERE `matches`.`id` = 10237 LIMIT 1
  Bar Load (0.2ms)  SELECT  `bars`.* FROM `bars`  ORDER BY `bars`.`id` ASC LIMIT 1
  SQL (0.2ms)  DELETE FROM `inform_requests` WHERE `inform_requests`.`match_id` = 10237 AND (lat < 1.48591 and lat > 1.0859100000000002) AND (lng < 104.05 and lng > 103.64999999999999) AND (`inform_requests`.`email` IS NOT NULL)
=> 1

在这种情况下,关系本身有1行,delete_all表示。 知道为什么会有这种差异? lat lng不合格因为它们是从字符串sql条件生成的。不确定这是否是问题..

0 个答案:

没有答案