3
我正在使用Rails 4引擎,並且通過正在創建的關聯有一個簡單的has_many,但在嘗試刪除關聯時似乎中斷。通過Rails 4中的關聯破壞has_many引擎無法找到列
我正在使用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
你能後的代碼'RolesController#update'?它可能會幫助... – Baldrick
嗨@ Baldrick-剛剛添加更新方法。希望有所幫助。 – johnkoht
這比我預期的要複雜得多......你能找到哪一行/操作正在生成錯誤的SQL請求嗎? – Baldrick