如何使用has_many:through关系编辑嵌套模型?

时间:2013-10-24 00:46:31

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord

我在轨道上4.让我说我有三个型号;房子,颜色和HouseColoring。

class House < ActiveRecord::Base
  has_many :house_colorings
  has_many :colors, through: :house_colorings
  accepts_nested_attributes_for :house_colorings, allow_destroy: true
end

class Color < ActiveRecord::Base
  has_many :house_colorings
  has_many :houses, through: :house_colorings
end

class HouseColoring < ActiveRecord::Base
  belongs_to :house
  belongs_to :color
end

我的房子控制器

class HousesController < ApplicationController
  before_action :set_house
  ...

  def new
    @house = House.new
    @house.house_colorings.build
  end

  def create
    @house = House.create(house_params)
    if @house.save
      redirect_to @house
    else
      render 'new'
    end
  end

  def edit
    #Gets @house from set_house
  end

  def update
    if @house.update(house_params)
      redirect_to @house
    else
      render 'edit'
    end
  end

  ...

  private

    def set_house
      @house = House.find(params[:id])
    end

    def house_params
      params.require(:house).permit(:some_parameters, house_coloring_attributes: [:color_id, :some_other_params])
    end
end

我在我的数据库中有一个颜色列表,可以在创建新房子时选择,房子可以有多种颜色。当我去创建一个新房子时,我选择了一种颜色,它可以保存得很好。我遇到的问题是我编辑我刚创建的房子并决定更改颜色,它会添加一个新的house_color记录,而不是更改我已经记录的那个。

这是我的_form.html.erb部分我家新编辑

<%= form_for @house do |f| %>
  <%= f.fields_for :house_colorings do |c| %>
    ....
    <%= c.collection_select :color_id, Color.all, :id, :name, {include_blank: "Select color"} %>
  <% end %>
<% end %>

为什么会这样?我需要更改什么才能更改/更新house_coloring而不是创建新记录?感谢。

修改

这是我尝试更新房屋时会发生什么的更新日志

Started PATCH "/MY_PATH/houses/16" for 127.0.0.1 at 2013-10-24 10:33:13 -0700
Processing by HousesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"sfdj43ksdkjsd3434fmskwec=", "house"=>{"name"=>"house", "house_colorings_attributes"=>{"0"=>{"color_id"=>"2", "id"=>"65"}, "1"=>{"color_id"=>"3", "id"=>"66"}, "2"=>{"color_id"=>"1", "id"=>"67"}}}, "commit"=>"Update House", "id"=>"16"}
  [1m[36mHouse Load (0.1ms)[0m  [1mSELECT "houses".* FROM "houses" WHERE "houses"."id" = ? LIMIT 1[0m  [["id", "16"]]
Unpermitted parameters: id
Unpermitted parameters: id
Unpermitted parameters: id
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36mSQL (1.0ms)[0m  [1mINSERT INTO "house_colorings" ("house_id", "created_at", "color_id", "updated_at") VALUES (?, ?, ?, ?, ?)[0m  [["house_id", 16], ["created_at", Thu, 24 Oct 2013 17:33:13 UTC +00:00], ["color_id", 2], ["updated_at", Thu, 24 Oct 2013 17:33:13 UTC +00:00]]
  [1m[35mSQL (0.3ms)[0m  INSERT INTO "house_colorings" ("house_id", "created_at", "color_id", "updated_at") VALUES (?, ?, ?, ?, ?)  [["house_id", 16], ["created_at", Thu, 24 Oct 2013 17:33:13 UTC +00:00], ["color_id", 3], ["updated_at", Thu, 24 Oct 2013 17:33:13 UTC +00:00]]
  [1m[36mSQL (0.1ms)[0m  [1mINSERT INTO "house_colorings" ("house_id", "created_at", "color_id", "updated_at") VALUES (?, ?, ?, ?, ?)[0m  [["house_id", 16], ["created_at", Thu, 24 Oct 2013 17:33:13 UTC +00:00], ["color_id", 1], ["updated_at", Thu, 24 Oct 2013 17:33:13 UTC +00:00]]
  [1m[35m (10.5ms)[0m  commit transaction
Redirected to http://localhost:3000/houses/16
Completed 302 Found in 24ms (ActiveRecord: 12.5ms)

1 个答案:

答案 0 :(得分:0)

我想通了,我需要在这样的参数中允许house_coloring的id

def house_params
      params.require(:house).permit(:some_parameters, house_coloring_attributes: [:id, :color_id, :some_other_params])
end

我希望这最终能帮助某人解决我所面临的问题。