rails/rails — Issue #4681: Regression in AR 3.2.0: "has_many through" with custom table name
Author: alexeymuranov
Created: 2012-01-26 00:05:14+00:00
State: open

The following code gives validation error in ActiveRecord 3.2.0, but worked fine in 3.1

```ruby
gem 'activerecord', '3.2.0' # Does not happen with activerecord 3.1.3
require 'active_record'
require 'logger'

# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"

ActiveRecord::Base.logger = Logger.new(STDOUT)

# Connect to an in-memory sqlite3 database (more on this in a moment)
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3',
:database => ':memory:' )

# Create the minimal database schema necessary to reproduce the bug
ActiveRecord::Schema.define do
create_table "users", :force => true do |t|
end

create_table :user_bugs, :force => true do |t|
t.integer :user_id
t.integer :bug_id
end

create_table :bugs, :force => true do |t|
end
end

# Create the minimal set of models to reproduce the bug
class User < ActiveRecord::Base
has_many :user_bugs
has_many :bugs, :through => :user_bugs
end

class UserBug < ActiveRecord::Base
self.table_name = :user_bugs # This causes the bug!
belongs_to :user
belongs_to :bug
validates :user_id, :presence => true
end

class Bug < ActiveRecord::Base
has_many :user_bugs
has_many :users, :through => :user_bugs
end

# Create some test data
user = User.create!
bug = Bug.create!

# Reproduce the actual bug!
user.bug_ids = [bug.id]
```

Gives:

```
...gems/activerecord-3.2.0/lib/active_record/validations.rb:56:in `save!': Validation failed: User can't be blank (ActiveRecord::RecordInvalid)
```