Skip to content

Conversation

@tcannonfodder
Copy link
Contributor

copy_attribute_definitions uses type_for_attribute

  • Using type_for_attribute seems to handle mapping certain ActiveRecord columns to ActiveModel types, notably TEXT => string in the example application
    • Unfortunately, we still need to use the columns_hash to pull the default value from the column's definition

fallback_to_model_values checks against original_attributes_hash

  • When an attribute has a default value, the check for if the attribute is falsey will be false, since it's using the default value
    • Instead, we want to actually check against the original_attributes_hash (the user-provided hash of attributes, such as the one used by initialize) to see if it had a value for that key
      • If so, we assume that the value has already been processed and does not need the fallback applied
  • Updated the tests to verify this behavior by setting a default for the body column and checking that
    • An empty model instance is initialized with the default
    • The model's fallback value is loaded if there was no value provided when initializing

* Using `type_for_attribute` seems to handle mapping certain
	`ActiveRecord` columns to `ActiveModel` types, notably `TEXT`
	=> `string` in the example application
	* Unfortunately, we still need to use the `columns_hash` to pull the
		default value from the column's definition
* When an attribute has a default value, the check for if the attribute
	is falsey will be false, since it's using the default value
	* Instead, we want to actually check against the
		`original_attributes_hash` (the user-provided hash of attributes,
		such as the one used by `initialize`) to see if it had a value for
		that key
		* If so, we assume that the value has already been processed and
			does not need the fallback applied
* Updated the tests to verify this behavior by setting a default for the
	`body` column and checking that
	* An empty model instance is initialized with the default
	* The model's fallback value is loaded if there was no value provided
		when initializing
@tcannonfodder tcannonfodder merged commit 4b3ae6b into main May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants