model build fails in the console when using 'after_initialize' callback in the model

211 views
Skip to first unread message

Javix

unread,
Mar 21, 2013, 5:19:09 AM3/21/13
to factor...@googlegroups.com
I tried to check one of my factories in Rails console and can't figure out why it fails.
Here is the model definition:
#timesheet.rb

class Timesheet < ActiveRecord::Base
  attr_accessible :status, :user_id, :start_date, :end_date, :activities_attributes
  
  SUBMITTED = 'Submitted'
  APPROUVED = 'Approuved'
  REJECTED = 'Rejected'
  
  STATUS_VALUES = [SUBMITTED, APPROUVED, REJECTED]

validates :status, presence: true, inclusion: {in: STATUS_VALUES}  
  
  after_update :check_an_activity_present
  
  after_initialize :init_working_week

private
    def init_working_week
      if start_date.blank? || end_date.blank?
        set_start_and_end_dates
      end
    end

    def set_start_and_end_dates
      filter_date = Date.today
      last_timesheet = Timesheet.last
      filter_date = last_timesheet.start_date.next_week if last_timesheet
      self.start_date = filter_date.beginning_of_week
      self.end_date = filter_date.end_of_week
    end
...
end

Here are factories:

#timesheets.rb
FactoryGirl.define do
  factory :timesheet do
    user
    
    factory :submitted do
      status Timesheet::SUBMITTED
      
      after(:build) do |submitted|
        timesheet.activities << FactoryGirl.build(:activity)
      end
    end
    
    factory :approuved do
      status Timesheet::APPROUVED
    end
    
    factory :rejected do
      status Timesheet::REJECTED
    end
  end
end

When I tried to create a timesheet in the console as follows:

Loading development environment in sandbox (Rails 3.2.12)
Any modifications you make will be rolled back on exit
irb(main):001:0> require 'factory_girl_rails'
=> true
irb(main):002:0> t = FactoryGirl.create(:submitted_timesheet)

I got the following error indicating that the STATUS validation fails:

ActiveRecord::RecordInvalid: Validation failed: Status can't be blank, Status is not included in the list
from /Users/serge-mac/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/validations.rb:56:in `save!'
from /Users/serge-mac/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
from /Users/serge-mac/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/transactions.rb:264:in `block in save!'

Any idea?
Thanks and regards

Javix

unread,
Mar 22, 2013, 4:19:39 AM3/22/13
to factor...@googlegroups.com
Fixed it, the problem was in association factory definition (activities.rb). So the modified version looks like that:

#spec/factories/timesheets.rb

FactoryGirl.define do
  factory :timesheet do
    user
    
    factory :submitted_timesheet do
      status Timesheet::SUBMITTED
      
      after(:build) do |submitted_timesheet|
        submitted_timesheet.activities << FactoryGirl.build(:activity, timesheet: submitted_timesheet)
      end
    end
    
    factory :approuved_timesheet do
      status Timesheet::APPROUVED
    end
    
    factory :rejected_timesheet do
      status Timesheet::REJECTED
    end
  end
end

#spec/factories/activities.rb

FactoryGirl.define do
  factory :activity do
    timesheet
    task
    
    after(:build) do |activity|
      activity.time_entries << FactoryGirl.build(:time_entry, activity: activity)
    end
  end
end

#spec/factories/time_entries.rb

FactoryGirl.define do
  factory :time_entry do
    activity    
    workdate Date.today.beginning_of_week
    worktime 1
  end
end

Now when creating a timesheet:

@timesheet = create(:submitted_timesheet) }

everything works as needed:  user, project, task, activity and time_entry created.

Hope this helps.
Reply all
Reply to author
Forward
0 new messages