Do not overwrite the database default value if provided by attributes#90
Do not overwrite the database default value if provided by attributes#90reuben453 wants to merge 1 commit intoFooBarWidget:masterfrom
Conversation
| def test_overwrites_string_blank_value_in_mass_assignment | ||
| Book.default_value_for(:type, :allows_nil => false) { 'string' } | ||
| assert_equal 'string', Book.new(type: '').type | ||
| end |
There was a problem hiding this comment.
I noticed that this case did not have a test, so added one here.
78b2524 to
f77e56d
Compare
When a new database record has a column that is set to its database
default value, the "#{attribute}_changed?" method returns false,
which causes default_value_for to overwrite the database default.
This commit changes this behavior to not overwrite when the column
is being set by the initialization attributes.
|
@chrisarcand Would you mind having a look at these changes please? 🙏 |
|
@reuben453 sorry you haven't had an update on this PR. I'm now maintaining it in my free time. Is this still an issue for you? I see the test does still fail when I run master code against it. I don't think the intention was to use column defaults with default_value_for. When I tested column defaults, it's great if it's supported in your database but it's limited in it's usage. On the other hand, it's in the database so likely more performant than doing things in ruby. I see you're intention was to use both but the default from the attributes should "win". I don't know if that's a good idea for this gem. I could be convinced though if you have use case to use both attribute defaults and default_value_for. My instincts say we should use attribute defaults where it works for us and use default_value_for to fill an use cases missing in attribute defaults. I'll keep this open for a week but I think I'll close it unless we can better describe good use cases for using both sets of defaults. Thanks! |
When a new database record has a column that is set to its database default value, the
"#{attribute}_changed?"method returns false, which causes theconnection_default_value_definedvalue to be true and causesdefault_value_forto overwrite the database default.This PR changes this behavior to not overwrite when the column is being set by the initialization attributes. It will continue to overwrite the database default if the attribute is not present in the initialization attributes.
Example code: