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

Fix inspection behavior when the :id column is not primary key #27935

Merged
merged 1 commit into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Fix inspection behavior when the :id column is not primary key.

*namusyaka*

* Deprecate locking records with unpersisted changes.

*Marc Schütz*
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def read_attribute(attr_name, &block)
attr_name.to_s
end

name = self.class.primary_key if name == "id".freeze
name = self.class.primary_key if name == "id".freeze && self.class.primary_key
_read_attribute(name, &block)
end

Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/primary_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "models/keyboard"
require "models/mixed_case_monkey"
require "models/dashboard"
require "models/non_primary_key"

class PrimaryKeysTest < ActiveRecord::TestCase
fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
Expand Down Expand Up @@ -89,6 +90,12 @@ def test_string_key
assert_equal("John Doe", subscriberReloaded.name)
end

def test_id_column_that_is_not_primary_key
NonPrimaryKey.create!(id: 100)
actual = NonPrimaryKey.find_by(id: 100)
assert_match %r{<NonPrimaryKey id: 100}, actual.inspect
end

def test_find_with_more_than_one_string_key
assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
end
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/non_primary_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class NonPrimaryKey < ActiveRecord::Base
end
4 changes: 4 additions & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,10 @@
create_table :test_with_keyword_column_name, force: true do |t|
t.string :desc
end

create_table :non_primary_keys, force: true, id: false do |t|
t.integer :id
end
end

Course.connection.create_table :courses, force: true do |t|
Expand Down