Skip to content

Commit c330596

Browse files
authored
Merge pull request #17 from bdurand/default-key-attribute-to-id
Default key attribute to id instead of looking up primary key
2 parents 031c914 + f5e1e19 commit c330596

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Allows data files to reference related records by instance name instead of forei
8787

8888
### Key Attribute Configuration
8989

90-
By default, uses model's `primary_key`. Override for non-id keys:
90+
By default, uses model's `id` column. Override for non-id keys:
9191

9292
```ruby
9393
self.support_table_key_attribute = :name # Use 'name' instead of 'id'

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Changed
1616

1717
- The default data directory is now set in a Railtie and can be overridden with the `config.support_table.data_directory` option in the Rails application configuration.
18-
- The `support_table_key_attribute` method now returns the primary key of the model if not explicitly set instead of implicitly interpreting `nil` as the primary key. This makes the behavior more consistent and explicit.
18+
- The `support_table_key_attribute` method now returns "id" if not explicitly set instead of implicitly interpreting `nil` as the primary key. This makes the behavior more consistent and explicit and avoids edge cases when running the code in environments where the database connection is not available. This is a breaking change if the table uses a primary key other than "id" and the `support_table_key_attribute` was not explicitly set to that primary key.
1919

2020
## 1.4.0
2121

lib/support_table_data.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ def support_table_key_attribute=(attribute_name)
4141
self._support_table_key_attribute = attribute_name&.to_s
4242
end
4343

44+
# Get the attribute used as the unique to identify records in the data files.
45+
#
46+
# @return [String] The name of the key attribute.
4447
def support_table_key_attribute
45-
_support_table_key_attribute || primary_key
48+
_support_table_key_attribute || "id"
4649
end
4750

4851
# Synchronize the rows in the table with the values defined in the data files added with

spec/models/group.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Group < ActiveRecord::Base
55

66
self.primary_key = :group_id
77

8+
self.support_table_key_attribute = :group_id
89
named_instance_attribute_helpers :group_id
910

1011
add_support_table_data "groups.yml"

0 commit comments

Comments
 (0)