加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 即使nested_attributes无效,update_attributes

发布时间:2020-12-17 02:00:32 所属栏目:百科 来源:网络整理
导读:我有2个嵌套数据模型: 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_i
我有2个嵌套数据模型:

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

在创建嵌套记录时,会进行正确的验证,例如,如果嵌套模型中的数据不正确,那么命令:

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

返回false

但如果更新数据:

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

永远是真的

即使嵌套数据无效.如果其中一个嵌套记录无效,则父级数据更新和值以及父级数据无效时返回false
保存时会忽略它们,并且update_attributes返回true.是否有机会在更新所有嵌套数据链时检查有效性数据?谢谢你的回复.

我正在使用:
Ruby 1.8.7
RoR 3.0.9
Mongoid 2.0.1

解决方法

请检查每个型号的“有效”功能以进行验证.
请在代码中添加以下代码:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读