Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Allows data files to reference related records by instance name instead of forei

### Key Attribute Configuration

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

```ruby
self.support_table_key_attribute = :name # Use 'name' instead of 'id'
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- 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.
- 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.
- 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.

## 1.4.0

Expand Down
5 changes: 4 additions & 1 deletion lib/support_table_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ def support_table_key_attribute=(attribute_name)
self._support_table_key_attribute = attribute_name&.to_s
end

# Get the attribute used as the unique to identify records in the data files.
#
# @return [String] The name of the key attribute.
def support_table_key_attribute
_support_table_key_attribute || primary_key
_support_table_key_attribute || "id"
end

# Synchronize the rows in the table with the values defined in the data files added with
Expand Down
1 change: 1 addition & 0 deletions spec/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Group < ActiveRecord::Base

self.primary_key = :group_id

self.support_table_key_attribute = :group_id
named_instance_attribute_helpers :group_id

add_support_table_data "groups.yml"
Expand Down