Rails autosave gives double validation errors -


i'm using rails 4.2.

i have 4 models:

class user < activerecord::base   belongs_to :organization   has_many :licenses end  class license < activerecord::base   belongs_to :user #, autosave: true end  class organization < activerecord::base   has_many :users end  class application < activerecord::base   has_many :licenses end 

then have code looks create license user:

def user   @_user ||= user.find(...) end  def create_license   license = license.find_or_initialize_by(application: @application, user: user)    if license.user.organization.nil?     license.user.organization = @organization   end    if license.user.organization == @organization     license.expires_on = nil     license.save   else     license.errors.add(:user, "user belongs different organization")   end    license end 

the problem code when run license.save not save user object, i.e. organization not changed.

so added belongs_to :user, autosave: true license class force save user well. works ok in case.

however, if have autosave option set , run code this:

user = user.new user.licenses.build(...) user.save 

the user object gets each validation error twice.

am doing weird?

you should remove autosave: true belongs_to. automatically save license well. have added autosave. running validations 2 times


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -