Rails的错误:accepts_nested_attributes_for没有更新我的has_many协会 [英] Rails bug: accepts_nested_attributes_for is not updating my has_many association

查看:96
本文介绍了Rails的错误:accepts_nested_attributes_for没有更新我的has_many协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个型号:

class Book < ActiveRecord::Base
  has_many :book_versions
  accepts_nested_attributes_for :book_versions, allow_destroy: true
  validates_associated :book_versions

class BookVersion < ActiveRecord::Base
  has_many :collection_items
  has_many :collections, through: :collection_items
  belongs_to :book
  validates_presence_of :price, :isbn #<-- validates presence

下面是我的PARAMS。请注意,我是如何离开book_version的价格名为 BB 空白。这应该是射击脱 validates_ presence_of:价格验证 BookVersion 模式(但不):

Here are my params. Notice how I leave the price of book_version with name bb blank. This should be firing off the validates_presence_of :price validation in the BookVersion model (but it doesn't):

书=> {标题=>ZZZZZZ,副标题=>,author_ids=>(),illustrator_ids=>(),award_ids= > [],theme_ids=>(),publication_date=>,imprint_id=>1,language_ids=>(),eng_vers_id=> book_versions_attributes=> {0=> {book_id=>2848,名=>ALT,书号=>,价格=>,famis_number =>,famis_price=>,weight_in_pounds=>},1=> {book_id=>2848,名称=>BB,ISBN= >123123123123,价格=>,famis_number=>,famis_price=>,weight_in_pounds=>1.0,库存=>08,身份证 =>1030}

"book"=>{"title"=>"zzzzzz", "subtitle"=>"", "author_ids"=>[""], "illustrator_ids"=>[""], "award_ids"=>[""], "theme_ids"=>[""], "publication_date"=>"", "imprint_id"=>"1", "language_ids"=>[""], "eng_vers_id"=>"", "book_versions_attributes"=>{"0"=>{"book_id"=>"2848", "name"=>"alt", "isbn"=>"", "price"=>"", "famis_number"=>"", "famis_price"=>"", "weight_in_pounds"=>""}, "1"=>{"book_id"=>"2848", "name"=>"bb", "isbn"=>"123123123123", "price"=>"", "famis_number"=>"", "famis_price"=>"", "weight_in_pounds"=>"1.0", "inventory"=>"08", "id"=>"1030"},

当我这样做 @ book.update_attributes(PARAMS [:书])在我的控制器,没有任何的 book_versions 更新,即使一切似乎都有效的:

When I do @book.update_attributes(params[:book]) in my controller, none of the book_versions update even though everything seems valid:

    >> @book.update_attributes(params[:book])
    => true
    >> @book.book_versions
    => [#<BookVersion id: 1030, book_id: 2848, name: "bb", isbn: "123123123123", inventory: "08", price: #<BigDecimal:7fc484885b80,'0.1122E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc4848861c0,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1031, book_id: 2848, name: "lb", isbn: "12312333333", inventory: "02", price: #<BigDecimal:7fc484886670,'0.2244E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc484886760,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1032, book_id: 2848, name: "hc", isbn: "111213213131", inventory: nil, price: #<BigDecimal:7fc4848869e0,'0.1212E4',9(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc484886d28,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>]
    >> @book.book_versions.map(&:price)
    => [#<BigDecimal:7fc484885b80,'0.1122E2',18(18)>, #<BigDecimal:7fc484886670,'0.2244E2',18(18)>, #<BigDecimal:7fc4848869e0,'0.1212E4',9(18)>]
    >> @book.book_versions.map(&:price).map(&:to_f)
    => [11.22, 22.44, 1212.0]
    >> @book.save
    => true
    >> @book.book_versions.map(&:price).map(&:to_f)
    => [11.22, 22.44, 1212.0] #<-- one of these should be `nil`.

这是怎么回事?该表格​​工作完全正常,当我创建一个图书许多 BookVersion 秒。但是,它不更新或验证任何东西,当我与现有的图书版本更新现有的书籍。

What's going on? The form works perfectly fine when I am creating a Book with many BookVersions. However, it does not update or validate anything when I'm updating an existing book with existing book versions.

这是一个延续在这里我的问题:<一href="http://stackoverflow.com/questions/21632576/activerecord-validates-associated-does-not-work-when-updating-model">ActiveRecord:更新模型时validates_associated不起作用?

This is a continuation of my question here: ActiveRecord: validates_associated does not work when updating model?

更新====

嗯...我认为这是在轨道中的错误?看看会发生什么:

Uh... I think it's a bug in rails? Look at what happens:

    >> @book.update_attributes(params[:book])
    => true
    >> @book.book_versions
    => [#<BookVersion id: 1030, book_id: 2848, name: "bb", isbn: "123123123123", inventory: "08", price: #<BigDecimal:7fc487ee9488,'0.1122E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc487ee9118,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1031, book_id: 2848, name: "lb", isbn: "12312333333", inventory: "02", price: #<BigDecimal:7fc487ee8f88,'0.2244E2',18(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc487ee8e98,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>, #<BookVersion id: 1032, book_id: 2848, name: "hc", isbn: "111213213131", inventory: nil, price: #<BigDecimal:7fc487ee8b50,'0.1212E4',9(18)>, famis_price: nil, famis_number: "", weight_in_pounds: #<BigDecimal:7fc487ee89c0,'0.1E1',9(18)>, width: nil, height: nil, thickness: nil>]
    >> @book.update_attributes(params[:book])
    => false

不过,这只是当我使用更好的错误和冻结之前, update_attributes的控制器。当我居然把 @ book.book_versions 控制器之前,我更新的属性,并尝试运行它,它仍然无法正常工作。

But that is only when I use better errors and freeze the controller before update_attributes. When I actually put @book.book_versions in the controller right before I update the attributes and try running it, it still doesn't work.

推荐答案

所以我想通了一些两轮牛车......出于某种原因,你必须首先装载在内存中。为了做到这一点,你需要的书版本的磁盘阵列上执行某种功能。只是打电话 book.book_versions 是不够的:

So I figured it out with some hackery... For some reason you have to load them in memory first. In order to do this you have to perform some sort of function on the array of book versions. just calling book.book_versions is not enough:

  @book.book_versions.sort_by(&:name) # this line is to load the book_versions, without it book_versions will not update!! it's a bug in rails I discovered
  if @book.update_attributes(params[:book]) #<-- returns false finally

这篇关于Rails的错误:accepts_nested_attributes_for没有更新我的has_many协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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