class Program < ActiveRecord::Base
has_many :contacts
class Contact < ActiveRecord::Base
belongs_to :program
FactoryGirl.define do
factory :contact do
sequence(....
...
program_id 1 #foreign key
program #association
(byebug) contact
Contact id: 949, display_name: "Contact-3", business_phone: "1234567894", fax_number: "1234567894", created_at: "2017-03-05 00:43:24", updated_at: "2017-03-05 00:43:24", first_name: "First-4", last_name: "Last-4", middle_initial: "4", email: "[email protected]", program_id: 1193, 287g: nil, active: true, call_office_id: 4
在聯繫工廠創建的聯繫人記錄中,program_id是1193,但程序表只有四個帶有ID 1-4的記錄。不知道1193是從哪裏來的。此時rspec測試或多或少地成功。但是,一旦下面的驗證代碼被添加到聯繫人模型中,rspec測試就會失敗。與協會確認新增工廠女協會驗證
接觸模型計劃
class ProgramValidator < ActiveModel::Validator
def validate(record)
if record.program.nil?
record.errors[:base] << "Program cannot be blank"
end
end
end
class Contact < ActiveRecord::Base
belongs_to :program
validates_with ProgramValidator
運行rspec的現在抱怨說,「程序不能爲空」。問題:如何創建聯繫工廠以滿足驗證?爲什麼社團如此艱難,比在ROR中創建社團更困難。謝謝閱讀。