diff --git a/AGENTS.md b/AGENTS.md index 028db65..b5e68ec 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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' diff --git a/CHANGELOG.md b/CHANGELOG.md index e939e31..c597bb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/support_table_data.rb b/lib/support_table_data.rb index 7e5a5d3..332abe4 100644 --- a/lib/support_table_data.rb +++ b/lib/support_table_data.rb @@ -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 diff --git a/spec/models/group.rb b/spec/models/group.rb index 4f0dd0c..b1d49d2 100644 --- a/spec/models/group.rb +++ b/spec/models/group.rb @@ -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"