2013-07-20 48 views
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 
+0

你能后的代码'RolesController#update'?它可能会帮助... – Baldrick

+0

嗨@ Baldrick-刚刚添加更新方法。希望有所帮助。 – johnkoht

+0

这比我预期的要复杂得多......你能找到哪一行/操作正在生成错误的SQL请求吗? – Baldrick

回答

2

你需要一个ID列添加到您的PermissionRole表。我更新了HABTM(不需要ID)的关联,并发现它造成了完全相同的问题。

这种迁移应该做的(显然产生迁移&改变你的规格):

add_column :image_products, :id, :primary_key 

这应该解决您的问题,如果相关依赖:destory元素到位

相关问题