轨道 - 更新nested_attributes不适用于一对多关系



我正在尝试实现nested_attributes并且创建时间表正在工作。但是它在编辑时失败而没有给出任何错误。它适用于一对一关系,但不适用于一对多关系。我正在传递时间表的id,如本文档中所述:嵌套属性

我有这些模型:

class Article < ActiveRecord::Base
  has_many :schedules
  accepts_nested_attributes_for :schedules
end
class Schedule < ActiveRecord::Base
  belongs_to :article
end

以下是将强参数列入白名单的控制器片段:

def article_params
  params.require(:article).permit(schedules_attributes: [ :id, :schedule_for, :content, :time_zone, :date, :time ])
end

这是我通过 rails 控制台尝试的示例

article_params = {"schedules_attributes"=>{"0"=>{"schedule_for"=>"pre", "content"=>"This is a scheduled message for ETS - !!!!!!!", "time_zone"=>"American Samoa", "date"=>"2013-10-20", "time"=>"11:15 AM", "id"=>"1"}}}
article = Article.find(1)
article.update(article_params)
D, [2013-10-25T16:42:50.266517 #2296] DEBUG -- :    (0.1ms)  BEGIN
D, [2013-10-25T16:42:50.267789 #2296] DEBUG -- :   Schedule Load (0.3ms)  SELECT "schedules".* FROM "schedules" WHERE "schedules"."article_id" = $1 AND "schedules"."id" IN (1)  [["article_id", 1]]
D, [2013-10-25T16:42:50.273288 #2296] DEBUG -- :    (0.3ms)  COMMIT

它选择了正确的计划,但没有执行更新查询,也没有任何错误。我在这里错过了什么吗?

编辑:

我正在使用Ruby 2.0和Rails 4。

这里有一些想法给你:

def article_params
  schedules_attributes: [ :id, :schedule_for, :content, :time_zone, :date, :time ]
end

应该是:

def article_params
    params.require(:article).permit(schedules_attributes: [ :id, :schedule_for, :content, :time_zone, :date, :time ])
end

这里的问题似乎是您没有使用强参数并且accepts_nested_attributes_for正确。

你能试试吗

accepts_nested_attributes_for :schedules

控制器文件

def article_params
    params.require(:article).permit(:my, :attributes, :schedules: [:schedule, :whitelist])
end
@article.update_attributes!(article_params)

对我来说,轨道会重新使用强参数和accepts_nested_attributes_for以这种方式工作是愚蠢的,但也许我错过了一些东西。

还可以尝试将.update_attributes!!一起使用,这将告诉轨道引发错误,以便您知道发生了什么。

你有article[schedules_attributes][0][schedule_for],我对此不是 100% 肯定的,但我相信这将作为 参数[:文章][:schedules_attributes_0][schedule_for]

你能试着把article[schedule_attributes][][attribute_name]

请查看文档中与您的问题相关的选项inverse_of

在模型中使用它:

class Article < ActiveRecord::Base
  has_many :schedules, inverse_of: :article
  accepts_nested_attributes_for :schedules
end
class Schedule < ActiveRecord::Base
  belongs_to :article, inverse_of: :schedules
end

我认为这里的问题与您如何测试它有关。如果您查看"一对多"下的文档 (http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html),您将看到以下示例,说明如何使用嵌套关联创建异议:

params = { member: {
  name: 'joe', posts_attributes: [
    { title: 'Kari, the awesome Ruby documentation browser!' },
    { title: 'The egalitarian assumption of the modern citizen' },
    { title: '', _destroy: '1' } # this will be ignored
  ]
}}
member = Member.create(params[:member])

您发布的示例没有将schedules_attributes作为Array传递,而是作为Hash传递,这似乎不是正确的用法。虽然这个例子是针对ActiveRecord::Base.create的,但这同样适用于ActiveRecord::Base#update_attributes

请参考此 PRO railscasts 进行嵌套关联...其伟大的RAILSCASTS PRO嵌套关联剧集

试试这个:

导轨控制台

 a = Article.find(1) 
 b = a.schedules.first
 article_params = {"schedules_attributes"=>{"0"=>{"schedule_for"=>"pre", "content"=>"This is a     scheduled message for ETS - !!!!!!!", "time_zone"=>"American Samoa", "date"=>"2013-10-20", "time"=>"11:15 AM", "id"=>"1"}}}
 b.update(article_params)
 b.save

您可能还想编辑文章参数,只是为了使其成为一个简单的对象。

最新更新