开发者

Rails: Need help with nested form rejecting nested object but still creating main object

开发者 https://www.devze.com 2023-03-18 09:30 出处:网络
开发者_高级运维I have the following in my user model class User < ActiveRecord::Base has_many :gym_users

开发者_高级运维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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号