default_value_for icon indicating copy to clipboard operation
default_value_for copied to clipboard

Do not overwrite the database default value if provided by attributes

Open reuben453 opened this issue 4 years ago • 2 comments

When a new database record has a column that is set to its database default value, the "#{attribute}_changed?" method returns false, which causes the connection_default_value_defined value to be true and causes default_value_for to 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:

ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
  t.integer :count, :null => false, :default => 1
end

Book.default_value_for(:count, :allows_nil => false) { 1234 }

Book.new(count: 1).count # This should return `1`. Currently, this returns `1234`. This PR fixes this.
Book.new.count # This should continue to return `1234`.

reuben453 avatar Aug 02 '21 06:08 reuben453

@chrisarcand Would you mind having a look at these changes please? :pray:

reuben453 avatar Aug 02 '21 08:08 reuben453