Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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
Mysql 仅删除Rails 4中表之间的连接记录_Mysql_Ruby On Rails_Ruby_Ruby On Rails 4_Has And Belongs To Many - Fatal编程技术网

Mysql 仅删除Rails 4中表之间的连接记录

Mysql 仅删除Rails 4中表之间的连接记录,mysql,ruby-on-rails,ruby,ruby-on-rails-4,has-and-belongs-to-many,Mysql,Ruby On Rails,Ruby,Ruby On Rails 4,Has And Belongs To Many,我有3种Rails型号: class User < ActiveRecord::Base has_and_belongs_to_many :char_factors end class CharFactor < ActiveRecord::Base has_and_belongs_to_many :users end class UserCharFact < ActiveRecord::Base belongs_to :user belongs_to :ch

我有3种Rails型号:

class User < ActiveRecord::Base
  has_and_belongs_to_many :char_factors
end

class CharFactor < ActiveRecord::Base
  has_and_belongs_to_many :users
end

class UserCharFact < ActiveRecord::Base
  belongs_to :user
  belongs_to :char_factor
end
上述措施似乎运作正常。但是我找不到删除两个表之间特定的
连接关系的方法。我尝试了以下方法:

def destroy
  @user_character = CharFactor.find(params[:id])
  @user.char_factors.delete(@user_character)
end

但它实际上是从
CharFactor
表中删除值,而不仅仅是删除关联

您删除它就像删除任何其他模型记录一样

user_char_factor = UserCharFactor.find_by(user_id: user_id, char_factor_id: char_factor_id)
user_char_factor.destroy if user_char_factor
UserCharFact.where(char\u-factor\u-id:params[:id],user\u-id:@user.id).全部销毁

user_char_factor = UserCharFactor.find_by(user_id: user_id, char_factor_id: char_factor_id)
user_char_factor.destroy if user_char_factor