简体   繁体   中英

update_attributes returns always true, even if nested_attributes are not valid

I have 2 models with nested data:

class Goodtender

  include Mongoid::Document
  include Mongoid::Timestamps

  field :name
  field :count
  references_many(:offerprices, :autosave => true)
  accepts_nested_attributes_for :offerprices, :allow_destroy => true, :reject_if => :all_blank

  validates_presence_of :name, :message => "Invalid"
  validates_numericality_of :count, :message => 'Invalid'
  validates_associated :offerprices, :message => 'Invalid'


end

class Offerprice

  include Mongoid::Document
  include Mongoid::Timestamps

  field :summ
  field :date_delivery, :type => DateTime
  field :note

  referenced_in :goodtender, :class_name => 'Goodtender'

  validates_presence_of :date_delivery, :message => "Invalid"
  validates_numericality_of :summ, :message => 'Invalid'

end

When making nested records, correct validation takes place, for example, if data in nested model do not correct, so command:

@tender = Tender.new(params[:tender])
@tender.save

returns false

but if update data:

@tender = Tender.find(params[:id])
@tender.update_attributes(params[:tender])

always eturns true

Even if nested data do not valid. Here parent's data updates and valids and if parents' data does not valid returns false, if one of the nested record does not valid, they are ignored when you are saving and update_attributes returns true. Is there the opportunity to check data on validity in the time of updating all the nested data chain? Thank you for your respond.

I'm using: Ruby 1.8.7 RoR 3.0.9 Mongoid 2.0.1

Please check "valid" function to each model for validate. Please add code as per below in your code :

@tender = Tender.find(params[:id]) <br/>
@tender.fieldname=params[:name] <br/>
if @tender.valid?  <br/>
   @tender.save <br/>
 end  <br/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM