我得到了這個模型:FactoryGirl給我一個錯誤
rails g model Absence user:references company:references from:date to:date date:date category:integer hours:decimal remarks
這也產生:
FactoryGirl.define do
factory :absence do
user nil
company nil
from nil
to nil
date nil
category 0
hours "8.00"
remarks "MyString"
end
end
我設置的,並日期到零,因爲它可以:從和還是有一定的日期。
當我嘗試這在我的規格:
@absence = create(:absence, user: @company.owner, from: "2015-09-10", to: "2015-09-10", hours: 4)
我收到此錯誤信息:
NoMethodError:
undefined method `from=' for #<Absence:0x007f81f5494b88>
出了什麼問題?
編輯: 當我從工廠/ absences.rb我得到它的下一個字段(到)去除
from nil
和刪除之後我看到在類別中的錯誤信息。
EDIT2: 型號:
class Absence < ActiveRecord::Base
belongs_to :user
belongs_to :company
enum type: {holiday: 0, sick: 1}
validates :from, presence: true, if: '!user_id.nil?'
validates :to, presence: true, if: '!user_id.nil?'
validates :date, presence: true, if: '!company_id.nil?'
validates :hours, presence: true, if: '!user_id.nil?'
validates :hours, :numericality => { :greater_than_or_equal_to => 0 }, if: '!user_id.nil?'
validates :category, presence: true, if: '!user_id.nil?'
validates_numericality_of :company_id, allow_nil: true
validates_numericality_of :user_id, allow_nil: true
validate :company_xor_user
validate :to_date_after_from_date
validate :hours_smaller_than_workday
validate :non_overlapping
after_save :calculate_time_checks
after_destroy :calculate_time_checks_delete
如果你只是做'@absence =創建(:缺席,用戶:@ company.owner,到:「2015-09-10」,小時:4)'沒有'from',它工作嗎? – patrickh003
你的模特是什麼樣的? –
你可以發佈你的模型代碼嗎? –