在 Rails 中,为什么保存新记录会返回 nil,而没有保存任何内容? [英] In Rails, why would saving a new record return nil, and nothing gets saved?

查看:22
本文介绍了在 Rails 中,为什么保存新记录会返回 nil,而没有保存任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用

au = AdUnit.new(cp)

au = AdUnit.new( cp )

其中 cp 等于:

{:name=>"second56", :description=>nil, :target_window=>"BLANK", :explicitly_targeted=>false, :ad_unit_sizes_attributes=>[{:height=>;90, :width=>728, :is_aspect_ratio=>false, :environment_type=>"BROWSER"}], :dfp_id=>"22319511", :parent_id_dfp="22261791"}

结果对象 au

#<AdUnit id:nil,dfp_id:22319511",parent_id_dfp:22261791",parent_id_bulk:nil,名称:second56",描述:nil,target_window:BLANK",explicit_targeted:false,created_at: nil, updated_at: nil>

此时,au.valid?au.new_record? 都返回 true.

At this point, au.valid? and au.new_record? both return true.

如果我执行 au.save(或 au.save!),结果是 nil 并且没有任何内容保存到数据库.但如果我这样做

If I do au.save (or au.save! for that matter) the result is nil and nothing is saved to the database. but if I do

aud = au.dup
aud.save

结果为 true 并保存记录.

the result is true and the record is saved.

我可以使用重复的解决方法保存我的对象,但这对我来说看起来很奇怪.任何人都可以就为什么会发生这种情况给出任何想法吗?下面是来自 Rails 控制台的 2 个保存语句的 SQL 片段.

I can save my object with the duplicate workaround, but this looks really weird to me. can anybody give any ideas as to why is this happening? Below the SQL fragments from the 2 save statements from Rails console.

提前感谢所有 Rails 大师.

Thanks in advance all Rails gurus.

返回 nil 并且不保存:

SQL (1.0ms) INSERT INTO "ad_units" ("created_at", "description", "dfp_id", "explicitly_targeted", "name", "parent_id_bulk", "parent_id_dfp", "target_window", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Wed, 04 Jan 2012 17:28:37 UTC +00:00], ["description", nil], ["dfp_id", "22400631"], ["explicitly_targeted", false], ["name", "zapiFirstLevel366"], ["parent_id_bulk", nil], ["parent_id_dfp", "1166751"],["target_window", "BLANK"], ["updated_at", 2012 年 1 月 4 日星期三 17:28:37 UTC +00:00]]=>无

返回 true 并保存:

SQL (0.9ms) INSERT INTO "ad_units" ("created_at", "description", "dfp_id", "explicitly_targeted", "name", "parent_id_bulk", "parent_id_dfp", "target_window", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Wed, 04 Jan 2012 17:29:58 UTC +00:00], ["description", nil], ["dfp_id", "22400631"], ["explicitly_targeted", false], ["name", "zapiFirstLevel366"], ["parent_id_bulk", nil], ["parent_id_dfp", "1166751"],["target_window", "BLANK"], ["updated_at", 2012 年 1 月 4 日星期三 17:29:58 UTC +00:00]]=>真实

推荐答案

好的,这是由于当 AdUnit 与 AdUnitSize 具有 has_and_belongs_to_many 关系时使用了 accepts_nested_attributes_for 造成的.所以我了解到这两个不能很好地协同工作.

OK, this was caused by the usage of accepts_nested_attributes_for when AdUnit has a has_and_belongs_to_many relationship with AdUnitSize. So I got to learn that these 2 do not work together nicely.

唯一的事情是,在某处有一个警告可能会很好,而不是必须从 .save 中找出它,在 valid?= true 对象而不是 true 或 false.

The only thing is, it might be good to have a warning somewhere, instead of having to find out about it from .save returning nil on a valid? = true object instead of true or false.

这篇关于在 Rails 中,为什么保存新记录会返回 nil,而没有保存任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆