我正在使用 Rails 4 引擎,我有一个简单的 has_many through 关联,它正在正确创建,但当我尝试删除关联时似乎中断了。
我正在使用 Rails 4、Formtastic 和 Cocoon。这是我的模型和关联。
# Role
module Kohcms
class Role < ActiveRecord::Base
has_many :permission_roles, dependent: :destroy
has_many :permissions, through: :permission_roles
accepts_nested_attributes_for :permissions, reject_if: proc { |attributes| attributes[:subject_class].blank? }, allow_destroy: true
end
end
# Permission
module Kohcms
class Permission < ActiveRecord::Base
has_many :permission_roles, dependent: :destroy
has_many :roles, through: :permission_roles
end
end
# Permission Role Join Model/Table
module Kohcms
class PermissionRole < ActiveRecord::Base
belongs_to :role
belongs_to :permission
end
end
当我添加新关联时,它工作正常。但是当我删除它时,我收到了这个错误:
ActiveRecord::StatementInvalid in Kohcms::Admin::RolesController#update
Mysql2::Error: Unknown column 'kohcms_permission_roles.' in 'where clause': DELETE FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`` = NULL
这是一个输出:
Started PATCH "/admin/roles/4" for 127.0.0.1 at 2013-07-20 14:46:11 -0500
Processing by Kohcms::Admin::RolesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Dnj2RDxlK7XJTf6NZLgmuIQCDOVfjhWjsN1mCPpHIn4=", "commit"=>"Update Role", "role"=>{"title"=>"asdfadsfadf", "permissions_attributes"=>{"0"=>{"_destroy"=>"1", "name"=>"asdf", "subject_class"=>"ActiveRecord::SchemaMigration", "subject_id"=>"", "action"=>"All", "id"=>"16"}}, "full_access"=>"0", "canlock"=>"0", "user_ids"=>[""]}, "id"=>"4"}
Kohcms::User Load (0.3ms) SELECT `kohcms_users`.* FROM `kohcms_users` WHERE `kohcms_users`.`id` = 1 ORDER BY `kohcms_users`.`id` ASC LIMIT 1
Kohcms::Role Load (0.3ms) SELECT `kohcms_roles`.* FROM `kohcms_roles` WHERE `kohcms_roles`.`id` = 1 ORDER BY `kohcms_roles`.`id` ASC LIMIT 1
Kohcms::Role Load (0.2ms) SELECT `kohcms_roles`.* FROM `kohcms_roles` WHERE `kohcms_roles`.`id` = 4 LIMIT 1
(0.1ms) BEGIN
Kohcms::User Load (0.3ms) SELECT `kohcms_users`.* FROM `kohcms_users` WHERE `kohcms_users`.`role_id` = 4
Kohcms::Permission Load (0.3ms) SELECT `kohcms_permissions`.* FROM `kohcms_permissions` INNER JOIN `kohcms_permission_roles` ON `kohcms_permissions`.`id` = `kohcms_permission_roles`.`permission_id` WHERE `kohcms_permission_roles`.`role_id` = 4 AND `kohcms_permissions`.`id` IN (16)
Kohcms::Role Exists (0.3ms) SELECT 1 AS one FROM `kohcms_roles` WHERE (`kohcms_roles`.`title` = BINARY 'asdfadsfadf' AND `kohcms_roles`.`id` != 4) LIMIT 1
Kohcms::PermissionRole Load (0.2ms) SELECT `kohcms_permission_roles`.* FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`role_id` = 4 AND `kohcms_permission_roles`.`permission_id` = 16
SQL (0.3ms) DELETE FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`` = NULL
Mysql2::Error: Unknown column 'kohcms_permission_roles.' in 'where clause': DELETE FROM `kohcms_permission_roles` WHERE `kohcms_permission_roles`.`` = NULL
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 11ms
提前致谢!
编辑 这是更新方法。这是一种共享方法,因此我的角色和权限控制器从另一个控制器继承:
def update
@parent = parent_model
@parents = parent_models
@model = fetch_model
@model = pre_update(@model)
if @model.errors.empty? and @model.update_attributes(permitted_params)
message = "#{@model.class.name.demodulize.titlecase} was successfully updated."
# allows for some basic controler specific functionality without redefining the create method
succeeding_update(@model)
respond_to do |format|
format.html {
if params[:redirect_to].present?
redirect_to params[:redirect_to], notice: message
else
redirect_to edit_model_link(@model), notice: message
end
}
format.json {
render_json_model_response @model, message, 'updated'
}
end
else
flash[:error] = 'There was an error, please try again.'
respond_to do |format|
format.html {
if params[:redirect_to].present?
redirect_to params[:redirect_to], notice: message
else
redirect_to edit_model_link @model
end
}
format.json { render_json_response :error, :notice => 'There was an error, please try again' }
end
end
end