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

时间:2022-04-02 01:16:09

I'm trying to implement nested_attributes and creating a schedule is working. But it fails upon editing without giving any errors. It works with one-to-one relationship but not for one-to-many. I'm passing the id of the schedule as it is stated in this doc: NestedAttributes

我正在尝试实现nested_attributes并创建一个计划正在运行。但它在编辑时失败而没有给出任何错误。它与一对一的关系有效,但不适用于一对多的关系。我正在传递日程表的ID,如本文档中所述:NestedAttributes

I have these models:

我有这些模型:

class Article < ActiveRecord::Base
  has_many :schedules

  accepts_nested_attributes_for :schedules
end

class Schedule < ActiveRecord::Base
  belongs_to :article
end

and here's a snippet of the controller for whitelisting strong parameters:

这里是控制器的片段,用于将强参数列入白名单:

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

And here is a sample which I tried via rails console:

这是我通过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

It selected the correct schedule but it didn't do an update query and didn't have any errors. Am I missing something here?

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

EDIT:

I am using Ruby 2.0 and Rails 4.

我正在使用Ruby 2.0和Rails 4。

6 个解决方案

#1


0  

Here are some ideas for you:

以下是一些想法:

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

Should be:

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

#2


0  

The problem here seems that you are not using strong parameters and accepts_nested_attributes_for correctly.

这里的问题似乎是你没有正确使用强参数和accepts_nested_attributes_for。

Can you try

你能试一下吗

model

accepts_nested_attributes_for :schedules

controller file

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

@article.update_attributes!(article_params)

It is idiotic to me that rails would reuire both strong parameters and accepts_nested_attributes_for to work in this way, but maybe I'm missing something.

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

Also try using .update_attributes! with a !, this will tell rails to raise errors, so you know whats going on.

还可以尝试使用.update_attributes!用!,这会告诉rails提出错误,所以你知道发生了什么。

You have article[schedules_attributes][0][schedule_for] in your for, I'm not 100% positive on this, but I believe that this will be sent as params[:article][:schedules_attributes_0][schedule_for]

你有for文章[schedules_attributes] [0] [schedule_for],我对此并不是100%肯定,但我相信这将作为params发送[:article] [:schedules_attributes_0] [schedule_for]

Can you try putting article[schedule_attributes][][attribute_name]

你能试试把文章[schedule_attributes] [] [attribute_name]

#3


0  

Please, look at the option inverse_of in documentation, that linked to your question

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

Use it in the models:

在模型中使用它:

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

#4


0  

I think the issue here has to do with how you are testing it. If you look at the documentation (http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html) under "One-to-many" you will see the following example of how to use the nested association for objection creation:

我认为这里的问题与你如何测试它有关。如果您查看“一对多”下的文档(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])

The example you posted does not pass the schedules_attributes as an Array, but instead as a Hash, which does not appear to be the proper usage. Although this example is for ActiveRecord::Base.create, the same should apply for ActiveRecord::Base#update_attributes.

您发布的示例不会将schedules_attributes作为数组传递,而是作为哈希传递,这似乎不是正确的用法。虽然此示例适用于ActiveRecord :: Base.create,但同样适用于ActiveRecord :: Base#update_attributes。

#5


0  

refer this PRO railscasts for nested association...its great RAILSCASTS PRO episode for nested association

参考这个PRO railscasts嵌套关联...它的嵌套关联的伟大的RAILSCASTS PRO剧集

#6


0  

Try this:

Rails Console

 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

You might also want to edit article params just to make it an easy object.

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

#1


0  

Here are some ideas for you:

以下是一些想法:

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

Should be:

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

#2


0  

The problem here seems that you are not using strong parameters and accepts_nested_attributes_for correctly.

这里的问题似乎是你没有正确使用强参数和accepts_nested_attributes_for。

Can you try

你能试一下吗

model

accepts_nested_attributes_for :schedules

controller file

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

@article.update_attributes!(article_params)

It is idiotic to me that rails would reuire both strong parameters and accepts_nested_attributes_for to work in this way, but maybe I'm missing something.

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

Also try using .update_attributes! with a !, this will tell rails to raise errors, so you know whats going on.

还可以尝试使用.update_attributes!用!,这会告诉rails提出错误,所以你知道发生了什么。

You have article[schedules_attributes][0][schedule_for] in your for, I'm not 100% positive on this, but I believe that this will be sent as params[:article][:schedules_attributes_0][schedule_for]

你有for文章[schedules_attributes] [0] [schedule_for],我对此并不是100%肯定,但我相信这将作为params发送[:article] [:schedules_attributes_0] [schedule_for]

Can you try putting article[schedule_attributes][][attribute_name]

你能试试把文章[schedule_attributes] [] [attribute_name]

#3


0  

Please, look at the option inverse_of in documentation, that linked to your question

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

Use it in the models:

在模型中使用它:

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

#4


0  

I think the issue here has to do with how you are testing it. If you look at the documentation (http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html) under "One-to-many" you will see the following example of how to use the nested association for objection creation:

我认为这里的问题与你如何测试它有关。如果您查看“一对多”下的文档(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])

The example you posted does not pass the schedules_attributes as an Array, but instead as a Hash, which does not appear to be the proper usage. Although this example is for ActiveRecord::Base.create, the same should apply for ActiveRecord::Base#update_attributes.

您发布的示例不会将schedules_attributes作为数组传递,而是作为哈希传递,这似乎不是正确的用法。虽然此示例适用于ActiveRecord :: Base.create,但同样适用于ActiveRecord :: Base#update_attributes。

#5


0  

refer this PRO railscasts for nested association...its great RAILSCASTS PRO episode for nested association

参考这个PRO railscasts嵌套关联...它的嵌套关联的伟大的RAILSCASTS PRO剧集

#6


0  

Try this:

Rails Console

 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

You might also want to edit article params just to make it an easy object.

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