On Sunday, March 22, 2020 at 9:00:37 AM UTC-7, Petr Kaleta wrote:
>
> Hello, it looks like there's an issue when using both auto_validations and 
> defaults_setter plugin.
>
> I am using sequel 5.30.0
>
> Please have a look on the following example:
>
> create_table :foo_bars do
> primary_key :id
>
> column :name, 'character varying(64)', null: false
> column :state, 'smallint', null: false
> end
>
> class FooBar < Sequel::Model(:foo_bars)
> plugin :auto_validations
> plugin :defaults_setter
>
> default_values[:state] = 1
> end
>
> FooBar.create # => Sequel::ValidationFailed: state is not present
>
> With validation_helpers plugin I have no issues and everything works as 
> expected...
>

defaults_setter's documentation states:

  The defaults_setter plugin makes the column getter methods return the 
default values for new objects, if the values have not already been set.

defaults_setter doesn't automatically set the values in the values hash 
before saving, but you can do that:

  class FooBar
    def before_validation
      super
      self.state ||= state
    end
  end

So this isn't a bug, it is expected behavior.  Note that older versions of 
defaults_setter (before Sequel 4) did eagerly set the default values when 
the model instance was created.  That was changed for performance reasons.

Note that the best way to fix your issue is to set 1 as the default for the 
state column in the database.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/e7a1da93-526e-433f-a6ac-ad87ddf8d891%40googlegroups.com.

Reply via email to