Skip to content

Commit 564a3d6

Browse files
committed
Support composite primary key in AR::Base#to_param
1 parent 37342a3 commit 564a3d6

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

activerecord/lib/active_record/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ class Base
331331
include Suppressor
332332
include Normalization
333333
include Marshalling::Methods
334+
335+
self.param_delimiter = "_"
334336
end
335337

336338
ActiveSupport.run_load_hooks(:active_record, Base)

activerecord/lib/active_record/integration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ module Integration
5555
# user = User.find_by(name: 'Phusion')
5656
# user_path(user) # => "/users/Phusion"
5757
def to_param
58-
# We can't use alias_method here, because method 'id' optimizes itself on the fly.
59-
id && id.to_s # Be sure to stringify the id for routes
58+
return unless id
59+
Array(id).join(self.class.param_delimiter)
6060
end
6161

6262
# Returns a stable cache key that can be used to identify this record.

activerecord/test/cases/integration_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "models/computer"
77
require "models/owner"
88
require "models/pet"
9+
require "models/cpk"
910

1011
class IntegrationTest < ActiveRecord::TestCase
1112
fixtures :companies, :developers, :owners, :pets
@@ -97,6 +98,32 @@ def test_to_param_with_no_arguments
9798
assert_equal "Firm", Firm.to_param
9899
end
99100

101+
def test_to_param_for_a_composite_primary_key_model
102+
assert_equal "1_123", Cpk::Order.new(id: [1, 123]).to_param
103+
end
104+
105+
def test_param_delimiter_changes_delimiter_used_in_to_param
106+
old_delimiter = Cpk::Order.param_delimiter
107+
Cpk::Order.param_delimiter = ","
108+
assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param)
109+
ensure
110+
Cpk::Order.param_delimiter = old_delimiter
111+
end
112+
113+
def test_param_delimiter_is_defined_per_class
114+
old_order_delimiter = Cpk::Order.param_delimiter
115+
old_book_delimiter = Cpk::Book.param_delimiter
116+
117+
Cpk::Order.param_delimiter = ","
118+
Cpk::Book.param_delimiter = ";"
119+
120+
assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param)
121+
assert_equal("1;123", Cpk::Book.new(id: [1, 123]).to_param)
122+
ensure
123+
Cpk::Order.param_delimiter = old_order_delimiter
124+
Cpk::Order.param_delimiter = old_book_delimiter
125+
end
126+
100127
def test_cache_key_for_existing_record_is_not_timezone_dependent
101128
utc_key = Developer.first.cache_key
102129

0 commit comments

Comments
 (0)