Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroy() breaking on model with has_and_belongs_to_many association to join table #16732

Closed
mwalsher opened this issue Aug 28, 2014 · 2 comments

Comments

@mwalsher
Copy link

Upgraded from rails 4.0.0 to 4.1.5 and ran into an issue when attempting to destroy an object that has a has_and_belongs_to_many association to another join table.

The model setup is as follows:

class Group < ActiveRecord::Base
  has_and_belongs_to_many :accounts_users
end
class AccountsUser < ActiveRecord::Base
  belongs_to :account
  belongs_to :user
end
class Account < ActiveRecord::Base
end
class User < ActiveRecord::Base
end

In the console:

g = Group.first
g.destroy()

Error:

ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect table name '': DELETE FROM `` WHERE `accounts_users_groups`.`group_id` = 14
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `query'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `execute'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/mysql2_adapter.rb:228:in `execute'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/mysql2_adapter.rb:254:in `exec_delete'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:106:in `delete'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/relation.rb:449:in `delete_all'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/associations/has_many_association.rb:122:in `delete_records'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/associations/collection_association.rb:255:in `delete'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/associations/collection_association.rb:198:in `delete_all'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/associations.rb:1596:in `destroy_associations'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.5/lib/active_record/persistence.rb:153:in `destroy'
... 17 levels...
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `require'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `block in require'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `require'
  from /Users/Walsh/Sites/myapp/bin/rails:8:in `<top (required)>'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
  from /Users/Walsh/.rvm/gems/ruby-2.1.2@myapp/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
  from /Users/Walsh/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  from /Users/Walsh/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
@mwalsher
Copy link
Author

Turned out to be a problem with the attribute_normalizer gem in version 1.1. 1.2 seems to fix the issue.

@joshnabbott
Copy link

Been looking for why my app broke for hours now. You have saved me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants