简体   繁体   English

Rails HABTM has_many销毁错误

[英]Rails HABTM has_many destroy error

I have a has_and_belongs_to_many using has_many between users and workspaces. 我有一个has_and_belongs_to_many在用户和工作区之间使用has_many

user.rb user.rb

class User < ActiveRecord::Base
  has_many :user_workspaces, dependent: :destroy
  has_many :workspaces, through: :user_workspaces 
  before_destroy :delete_workspaces

 def delete_workspaces
   self.workspaces.each(&:destroy)
 end 

workspace.rb workspace.rb

class Workspace < ActiveRecord::Base
  has_many    :user_workspaces
  has_many    :users, through :user_workspaces
end

user_workspaces class and migration: user_workspaces类和迁移:

class UserWorkspace < ActiveRecord::Base 
  self.table_name  = "user_workspaces"
  belongs_to  :user 
  belongs_to  :workspace
end

class CreateUsersAndWorkspaces < ActiveRecord::Migration
  def change
    create_table :users_workspaces, id: false do |t|
      t.belongs_to :user, index: true
      t.belongs_to :workspace, index: true
      t.boolean :admin, default: true  
    end
  end
end
    class RenameUsersWorkspaces < ActiveRecord::Migration
  def change
    rename_table('users_workspaces', 'user_workspaces')
  end
end

I want this both test to pass: 我希望这两个测试都通过:

should "destroy all associatios and objects" do 
  user = User.create!(attributes_for(:user))
  w = user.workspaces.create!(attributes_for(:workspace))
  user.destroy 
  assert UserWorkspace.all.empty? #asser there are no associations
end 

which gives me the error 这给了我错误

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: user_workspaces.: DELETE FROM "user_workspaces" WHERE "user_workspaces"."" = ? ActiveRecord :: StatementInvalid:SQLite3 :: SQLException:否这样的列:user_workspaces .:从“ user_workspaces”中删除“ user_workspaces”。“” =?

 should "destroy association when destroying workspace" do
      user = User.create!(attributes_for(:user))
      w = user.workspaces.create!(attributes_for(:workspace))
      w.destroy
      assert UserWorkspace.all.empty?
 end

How can I cover both scenarios? 如何涵盖这两种情况? destroying an user destroys the association and the workspaces, destroying the workspace also destroys the association 销毁用户会销毁关联和工作区,销毁工作区也会销毁关联

According to http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html just need to ignore the join table. 根据http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html,只需忽略连接表即可。

Workspace.rb now has dependent destroy, to destroy only the join (intended behavior of destroy) Workspace.rb现在具有从属销毁,仅销毁联接(销毁的预期行为)

  has_many    :users,             through:      :user_workspaces,
                                  inverse_of:   :workspaces,
                                  dependent:    :destroy

then user.rb keeps the delete_workspaces functions, but adds dependent: :destroy, to also destroy the association. 然后user.rb保留delete_workspaces函数,但添加dependent::destroy也破坏该关联。

user.rb user.rb

    before_destroy: :delete_workspaces
    has_many :workspaces,           through: :user_workspaces,                                   
                                     dependent: :destroy

 def delete_workspaces
   self.workspaces.each(&:destroy)
 end 

now both test passes. 现在两个测试都通过了。

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

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