How to accept terms (non-attribute field) in Fabrication?
Closed this issue · 1 comments
oasisweng commented
I want to fabricate my Staff model that has a virtual variable terms
to ensure users to accept terms and conditions when they sign up. However, terms
is not a column for Staff table. I know I can use if
to make terms
check a conditional validation. But it will give me Terms must be accepted
error if I don't.
What should I do, in this case, to assign a value to a virtual variable such as terms
to allow Fabricate(:staff) to pass the validate_acceptance_of
test?
My Staff.rb
looks like this:
class Staff < ActiveRecord::Base
# Virtual Variable
attr_accessor :terms
...
validates_acceptance_of :terms
end
I made a Staff fabrication like this:
Fabricator(:staff) do
profile { }
work_no { }
position {}
phone_no {}
password {}
before_validation {
|staff, transients|
staff.terms = true
}
end
When I call Fabricate(:staff)
from rails console, it gives me this error:
ActiveRecord::RecordInvalid: Validation failed: Terms must be accepted
paulelliott commented
You should just need to add it to your fabricator definition.
Fabricator(:staff) do
terms true
end