johnkoht
johnkoht

Reputation: 602

Destroy has_many through association in Rails 4 Engine cannot find the column

I'm working on a Rails 4 engine and I have a simple has_many through association that is being created correctly but seems to break when I try to remove the association.

I'm using Rails 4, Formtastic and Cocoon. Here are my models and associations.

# 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

When I add the new association, it works fine. But when I delete it, I get this error:

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

Here is an output:

    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

Thanks in advance!

EDIT Here is the update method. It's a shared method so my Roles and Permissions controllers inherit from another controller with this:

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

Upvotes: 3

Views: 720

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

You need to add an ID column to your PermissionRole table. I updated my association from HABTM (where you don't need an ID), and found that it was causing exactly the same problem.

This migration should do (obviously generate migration & change to your specs):

  add_column :image_products, :id, :primary_key

This should resolve your issue if the relevant dependent: :destory elements are in place

Upvotes: 2

Related Questions