Rails: Need help with nested form rejecting nested object but still creating main object
开发者_高级运维I have the following in my user model
class User < ActiveRecord::Base
has_many :gym_users
attr_accessible :gym_users_attributes, :gym_users
accepts_nested_attributes_for :gym_users, :reject_if => lambda { |a| a[:role_id].blank? }
end
This correctly rejects the gym_user
model if the role_id
is not present, the problem is it still creates the user and simply doesn'
t create the gym_user
. Is there a way to make it not create or delete the user when the gym_user
is rejected?
You can add
validates_associated :gym_users
to your User
model and move validation from reject_if
to GymUsers
model
validates_presence_of :role_id
Add validates :gym_users, :presence => true
to your User model
精彩评论