An Organization model has a 1:many association with a User model. I have the following validation in my User model file:
belongs_to :organization validates_presence_of :organization_id, :unless => 'usertype==1'
If usertype is 1, it means the user will have no organization associated to it. For a different usertype the presence of an organization_id should be mandatory.
The organization model includes:
has_many :users accepts_nested_attributes_for :users, :reject_if => :all_blank, :allow_destroy => true
My seeds file uses nesting and includes:
Organization.create!(name: "Fictious business", address: Faker::Address.street_address, city: Faker::Address.city, users_attributes: [email: "[email protected]", username: "helpyzghtst", usertype: 2, password: "foobar", password_confirmation: "foobar"])
On seeding this generates the error below. Removing the validation from the model solves it, but I don't want to do that. How can I solve this?
Validation failed: Users organization can't be blank