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