Skip to content

Commit

Permalink
Merge pull request #10500 from kennyj/fix_10450
Browse files Browse the repository at this point in the history
Fixed a bug in  when using has_many association with :inverse_of option and UUID primary key.

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Sep 22, 2013
2 parents e504cd2 + abf3fef commit 7f2e645
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
* Fixed a bug in `ActiveRecord::Associations::CollectionAssociation#find_by_scan`
when using `has_many` association with `:inverse_of` option and UUID primary key.

Fixes #10450.

*kennyj*

* ActiveRecord::Base#<=> has been removed. Primary keys may not be in order, * ActiveRecord::Base#<=> has been removed. Primary keys may not be in order,
or even be numbers, so sorting by id doesn't make sense. Please use `sort_by` or even be numbers, so sorting by id doesn't make sense. Please use `sort_by`
and specify the attribute you wish to sort with. For example, change: and specify the attribute you wish to sort with. For example, change:
Expand Down
Expand Up @@ -554,14 +554,14 @@ def include_in_memory?(record)
# specified, then #find scans the entire collection. # specified, then #find scans the entire collection.
def find_by_scan(*args) def find_by_scan(*args)
expects_array = args.first.kind_of?(Array) expects_array = args.first.kind_of?(Array)
ids = args.flatten.compact.map{ |arg| arg.to_i }.uniq ids = args.flatten.compact.map{ |arg| arg.to_s }.uniq


if ids.size == 1 if ids.size == 1
id = ids.first id = ids.first
record = load_target.detect { |r| id == r.id } record = load_target.detect { |r| id == r.id.to_s }
expects_array ? [ record ] : record expects_array ? [ record ] : record
else else
load_target.select { |r| ids.include?(r.id) } load_target.select { |r| ids.include?(r.id.to_s) }
end end
end end


Expand Down
40 changes: 40 additions & 0 deletions activerecord/test/cases/adapters/postgresql/uuid_test.rb
Expand Up @@ -93,3 +93,43 @@ def test_id_allows_default_override_via_nil
assert_nil col_desc["default"] assert_nil col_desc["default"]
end end
end end

class PostgresqlUUIDTestInverseOf < ActiveRecord::TestCase
class UuidPost < ActiveRecord::Base
self.table_name = 'pg_uuid_posts'
has_many :uuid_comments, inverse_of: :uuid_post
end

class UuidComment < ActiveRecord::Base
self.table_name = 'pg_uuid_comments'
belongs_to :uuid_post
end

def setup
@connection = ActiveRecord::Base.connection
@connection.reconnect!

@connection.transaction do
@connection.create_table('pg_uuid_posts', id: :uuid) do |t|
t.string 'title'
end
@connection.create_table('pg_uuid_comments', id: :uuid) do |t|
t.uuid :uuid_post_id, default: 'uuid_generate_v4()'
t.string 'content'
end
end
end

def teardown
@connection.transaction do
@connection.execute 'drop table if exists pg_uuid_comments'
@connection.execute 'drop table if exists pg_uuid_posts'
end
end

def test_collection_association_with_uuid
post = UuidPost.create!
comment = post.uuid_comments.create!
assert post.uuid_comments.find(comment.id)
end
end

0 comments on commit 7f2e645

Please sign in to comment.