Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord dependent::destroy在简单场景中不起作用_Activerecord_Ruby On Rails 4 - Fatal编程技术网

Activerecord dependent::destroy在简单场景中不起作用

Activerecord dependent::destroy在简单场景中不起作用,activerecord,ruby-on-rails-4,Activerecord,Ruby On Rails 4,虽然它直截了当,但它不起作用 class Company < ActiveRecord::Base has_many :users, dependent: :destroy end class User < ActiveRecord::Base belongs_to :company end Company.find(39).destroy() # what I expect is to destroy all users that has company_id equal

虽然它直截了当,但它不起作用

class Company < ActiveRecord::Base
  has_many :users, dependent: :destroy
end
class User < ActiveRecord::Base
  belongs_to :company
end

Company.find(39).destroy()
# what I expect is to destroy all users that has company_id equals 39, 
#but that won't happen
怎么了

编辑,以下是我在rails c中的操作:

[88] pry(main)> User.create!(email: "as3now29@gmail.com", company_id: 29, password:"1234567890")
   (0.2ms)  begin transaction
  User Exists (0.2ms)  SELECT  1 AS one FROM "users"  WHERE "users"."email" = 'as3now29@gmail.com' LIMIT 1
Binary data inserted for `string` type on column `encrypted_password`
  SQL (0.3ms)  INSERT INTO "users" ("company_id", "created_at", "email", "encrypted_password", "updated_at") VALUES (?, ?, ?, ?, ?)  [["company_id", 29], ["created_at", "2014-09-02 08:59:29.702141"], ["email", "as3now29@gmail.com"], ["encrypted_password", "$2a$10$FHsDMbEhmXyNZGz685inSOv6f7meDkxyN2rglmc.99F.lZeYOnTCG"], ["updated_at", "2014-09-02 08:59:29.702141"]]
   (51.2ms)  commit transaction
=> #<User id: 42, email: "as3now29@gmail.com", encrypted_password: "$2a$10$FHsDMbEhmXyNZGz685inSOv6f7meDkxyN2rglmc.99F...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2014-09-02 08:59:29", updated_at: "2014-09-02 08:59:29", number: nil, name: nil, car_id: nil, company_id: 29, hardware_id: nil>
[89] pry(main)> Company.find(29).destroy
  Company Load (0.1ms)  SELECT  "companies".* FROM "companies"  WHERE "companies"."id" = ? LIMIT 1  [["id", 29]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  DELETE FROM "companies" WHERE "companies"."id" = ?  [["id", 29]]
   (12.4ms)  commit transaction
=> #<Company id: 29, created_at: "2014-09-02 08:59:16", updated_at: "2014-09-02 08:59:16", user_id: nil, admin_email: nil, name: nil>
[91] pry(main)> User.all.select(:id, :number, :email, :company_id, :hardware_id)
  User Load (0.3ms)  SELECT "users"."id", "users"."number", "users"."email", "users"."company_id", "users"."hardware_id" FROM "users"
=> [ #<User id: 42, email: "as3now29@gmail.com", number: nil, company_id: 29, hardware_id: nil>]

好的,我找到了答案,我必须从公司中删除user_id列,否则它将不起作用,因此必须有一个链接表将公司与所有者用户链接起来

如果使用不带括号的destroy怎么办?我马上检查!同样的结果,它不会破坏用户。这很奇怪,因为代码看起来很好。您可以肯定,有些用户确实有company_id==39?它会毁掉公司还是什么都不做?它会毁掉公司,但用户还在那里。。一定是出了什么事,,