Skip to content

Commit

Permalink
Merge pull request #27566 from npezza93/singular_collection_ids-fix
Browse files Browse the repository at this point in the history
Fix collection_singular_ids= ignores different primary key on relationhip
  • Loading branch information
rafaelfranca committed Jan 4, 2017
2 parents 158a856 + ae83c5b commit 079f3f3
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
Expand Up @@ -61,10 +61,17 @@ def ids_reader

# Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
def ids_writer(ids)
pk_type = reflection.primary_key_type
ids = Array(ids).reject { |id| id.blank? }
ids.map! { |i| pk_type.type_cast_from_user(i) }
replace(klass.find(ids).index_by { |r| r.id }.values_at(*ids))
pk_column = reflection.association_primary_key
pk_type = klass.type_for_attribute(pk_column)
ids = Array(ids).reject(&:blank?).map do |i|
pk_type.type_cast_from_user(i)
end

if (objs = klass.where(pk_column => ids)).size == ids.size
replace(objs.index_by { |r| r.send(pk_column) }.values_at(*ids))
else
objs.raise_record_not_found_exception!(ids, objs.size, ids.size)
end
end

def reset
Expand Down
Expand Up @@ -26,12 +26,15 @@
require 'models/membership'
require 'models/club'
require 'models/organization'
require 'models/user'
require 'models/user_business'
require 'models/business'

class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags,
:owners, :pets, :toys, :jobs, :references, :companies, :members, :author_addresses,
:subscribers, :books, :subscriptions, :developers, :categorizations, :essays,
:categories_posts, :clubs, :memberships, :organizations
:categories_posts, :clubs, :memberships, :organizations, :users, :businesses

# Dummies to force column loads so query counts are clean.
def setup
Expand Down Expand Up @@ -1201,4 +1204,11 @@ def test_has_many_through_do_not_cache_association_reader_if_the_though_method_h
ensure
TenantMembership.current_member = nil
end

def test_singular_collection_ids_are_set_correctly
@user = users(:one)
assert @user.businesses.empty?
@user.business_ids = [businesses(:walmart).uuid]
assert @user.businesses.include?(businesses(:walmart))
end
end
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/businesses.yml
@@ -0,0 +1,3 @@
walmart:
name: walmart
uuid: <%= SecureRandom.uuid %>
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/users.yml
@@ -0,0 +1,2 @@
one:
name: Bob Smith
2 changes: 2 additions & 0 deletions activerecord/test/models/business.rb
@@ -0,0 +1,2 @@
class Business < ActiveRecord::Base
end
4 changes: 4 additions & 0 deletions activerecord/test/models/user.rb
@@ -0,0 +1,4 @@
class User < ActiveRecord::Base
has_many :user_businesses
has_many :businesses, through: :user_businesses
end
4 changes: 4 additions & 0 deletions activerecord/test/models/user_business.rb
@@ -0,0 +1,4 @@
class UserBusiness < ActiveRecord::Base
belongs_to :user
belongs_to :business, primary_key: :uuid
end
14 changes: 14 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -798,6 +798,20 @@ def except(adapter_names_to_exclude)
t.integer :car_id
end

create_table :users, force: true do |t|
t.string :name
end

create_table :user_businesses, force: true do |t|
t.belongs_to :user
t.string :business_id, index: true
end

create_table :businesses, force: true do |t|
t.string :name
t.string :uuid
end

create_table :variants, force: true do |t|
t.references :product
t.string :name
Expand Down

0 comments on commit 079f3f3

Please sign in to comment.