ruby on rails 4 - How to auto rollback parent record if child record is not valid in has_one association -
here 2 models (rails 4.2) customer
, address
. customer
has_one
address
.
class customer < activerecord::base has_one :address, autosave: true, dependent: :destroy end class address < activerecord::base belongs_to :customer validates :add_line, :presence => true end
with autosave
set true, address
saved along customer
. if address
not valid, don't want save customer
@ all. our question how set has_one
association such rollback of customer
happens automatically if address
not valid?
use validates_associated
validate address before save customer.
class customer < activerecord::base has_one :address, autosave: true, dependent: :destroy validates_associated :address end
Comments
Post a Comment