Ruby on rails 具有多个对象属性的Rails散列。我该如何拯救他们?

Ruby on rails 具有多个对象属性的Rails散列。我该如何拯救他们?,ruby-on-rails,forms,hash,save,Ruby On Rails,Forms,Hash,Save,我从一个字段\u中获取哈希值,如下所示: "affiliation_attributes"=>{ "11"=>{"volunteer_id"=>"14", "affiliationtype_id"=>"1", "organization_id"=>"1"}, "1"=>{"volunteer_id"=>"1", "affiliationtype_id"=>"3", "organization_id"=>"1"}, "4"=>{"v

我从一个字段\u中获取哈希值,如下所示:

"affiliation_attributes"=>{
"11"=>{"volunteer_id"=>"14", "affiliationtype_id"=>"1", "organization_id"=>"1"},

"1"=>{"volunteer_id"=>"1", "affiliationtype_id"=>"3", "organization_id"=>"1"}, 

"4"=>{"volunteer_id"=>"2", "affiliationtype_id"=>"3", "organization_id"=>"1"}, 

"21"=>{"volunteer_id"=>"20", "affiliationtype_id"=>"1", "organization_id"=>"1"}
像11这样的第一个数字是affiliationtype_id,它旁边的值是它的属性

我想做的是保存所有这些类型。 我该怎么做


提前谢谢。

好吧,我找到了一种方法,但这只是一种临时的黑客行为

我提出了一种方法,可以做到这一点:


 affiliation_attributes.each do |attributes|

      a = Affiliation.find_by_id(attributes[0])
      a.volunteer_id = attributes[1]["volunteer_id"]
      a.organization_id = attributes[1]["organization_id"]
      a.affiliationtype_id = attributes[1]["affiliationtype_id"]
      a.save

  end
我怎样才能让这更好?
谢谢。

您需要接受嵌套的属性

如果您以通常的方式填写表单,您应该查看railscasts.com上的复杂表单1到3,本案例在那里处理。

或者,如果你想走简单的路,使用formtastic,这是为你处理的;- 阅读以下示例:for=>