3

私は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

前もって感謝します!

EDIT ここに更新方法があります。これは共有メソッドであるため、私の Roles and Permissions コントローラーは次のように別のコントローラーから継承します。

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
4

1 に答える 1

2

テーブルに ID 列を追加する必要がありPermissionRoleます。HABTM (ID は必要ありません) から関連付けを更新したところ、まったく同じ問題が発生していることがわかりました。

この移行は行う必要があります(明らかに移行を生成し、仕様に変更します):

  add_column :image_products, :id, :primary_key

関連するdependent: :destory 要素が配置されている場合、これで問題が解決するはずです。

于 2013-09-20T16:14:51.220 に答える