solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Default values aren't assigned when initialize method is present. #373

Open prasadsurase opened 7 years ago

prasadsurase commented 7 years ago

I am using rails 5.0.2 with virtus 1.0.5. I have a form defined as below

class MyForm
  include Virtus.model

  attribute :age, Integer, default: 0
  attribute :birthday, DateTime, default: :some_date
  attribute :salutation, String, default: 'Mr.'

  def initialize
  end

  private

  def some_date
    Date.today - 3.years
  end
end

If the initialize method is removed, the default values are present when an instance of MyForm is created. The values are absent if the initialize is present.

joshmn commented 7 years ago

Did you try passing you pass lazy => true to your attribute?