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
Ruby on rails 4 Rails-4.2删除“通过”关联_Ruby On Rails 4_Rails Activerecord_Model Associations - Fatal编程技术网

Ruby on rails 4 Rails-4.2删除“通过”关联

Ruby on rails 4 Rails-4.2删除“通过”关联,ruby-on-rails-4,rails-activerecord,model-associations,Ruby On Rails 4,Rails Activerecord,Model Associations,在两个主表的索引值已知的情况下,销毁/中断关联的最简单语法是什么 User has_many roles through clearances Role has_many users through clearances Clearance[.id, .user_id, .role_id] Role[.id, .rolename] User[.id, .username] username == 'auser' rolename == 'arole' 我希望删除连接这两个值的间隙行,但不接

在两个主表的索引值已知的情况下,销毁/中断关联的最简单语法是什么

User has_many roles through clearances
Role has_many users through clearances

Clearance[.id, .user_id, .role_id]
Role[.id, .rolename]
User[.id, .username]

username == 'auser'
rolename == 'arole'
我希望删除连接这两个值的间隙行,但不接触任何一个主表

2015-07-08 适合我的解决方案是:

  mu = User.find_by!( :username => uname )
  mr = Role.find_by!( :rolename => rname )
  rc = mu.authorisations.find_by( :role_id => mr.id )
  rc.delete if rc
下面@Hristo的建议导致此错误:

SQLite3::SQLException:没有这样的列:authorizations.username:选择authorizations.*来自authorizations,其中authorizations.username='xtra_user'和authorizations.rolename='test_role'ActiveRecord::StatementInvalid

注意:由于这是一个正在积极开发的项目,因此术语“清除” 已由授权取代,并对模型进行了重命名和编辑 因此

正如在原始问题中所指出的,联接表只有模型的引用id值

Clearance.where(age: "auser", rolename: "arole").destroy_all


应该能够做到并且足够简短。

我回答了你的问题吗非常感谢。你可能有,但我没有机会证明这一点。出于某种原因,我没有收到你的回复邮件,只是为了你的跟进。我必须继续工作。当我证明这个公式确实有效时,我会将问题标记为已回答。不,这不起作用。我使用的解决方案是:
Clearance.destroy_all(age: "auser", rolename: "arole")