父模型在表单中更新子模型时的对象ID为零

时间:2013-08-20 23:56:33

标签: ruby-on-rails ruby associations rails-models

我正在尝试使用连接表council_history保存父模型ID及其子属性,由于某种原因,它似乎没有在连接表中保存父模型ID但是当我尝试做某事时保存子属性ID类似于多对多关联一切正常,父对象被保存,我不确定它的关联是否错误,已经与多种组合摔跤,任何帮助都会受到赞赏。

Started PUT "/properties/34/build/council" for 127.0.0.1 at 2013-08-21 00:44:37 +0100
Processing by Properties::BuildController#update as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"+nfsCrRvTUrgYrdHix0v2QGaDJj25j4X1sh8oHVpkgs=", "property"=>    {"council_history_attributes"=>{"property_id"=>"34", "council_id"=>"1"}}, "commit"=>"Create    Council history", "property_id"=>"34", "id"=>"council"}



Started GET "/properties/34/build/confirmed" for 127.0.0.1 at 2013-08-21 00:44:37 +0100
Processing by Properties::BuildController#show as HTML
  Parameters: {"property_id"=>"34", "id"=>"confirmed"}
  Property Load (0.2ms)  SELECT "properties".* FROM "properties" WHERE "properties"."id" = ? LIMIT 1  [["id", "34"]]

  CouncilHistory Load (0.3ms)  SELECT "council_histories".* FROM "council_histories" WHERE "council_histories"."property_id" = 34 LIMIT 1
   (0.1ms)  begin transaction
   **(0.5ms)  UPDATE "council_histories" SET "property_id" = NULL, "updated_at" = '2013-08-20 23:44:37.220619' WHERE "council_histories"."id" = 18**
   (1.5ms)  commit transaction
  Tenant Load (0.3ms)  SELECT "tenants".* FROM "tenants" WHERE "tenants"."property_id" = 34
  Rendered properties/build/confirmed.html.erb within layouts/application (2.1ms)
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
   (0.4ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" =   "users_roles"."role_id" WHERE "users_roles"."user_id" = 3 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))

Property.rb

class Property < ActiveRecord::Base
  attr_accessible  :name, :council_history_attributes, :council,
   :property_id, :council_id, :status, :council_history_id
  belongs_to :user

  has_one :council_history 
  accepts_nested_attributes_for :council_history


  has_one :council, through: :council_history, :foreign_key => :council_id
  accepts_nested_attributes_for :council

Council.rb

class Council < ActiveRecord::Base
  attr_accessible :CouncilEmail, :name, :CouncilTel, :council_histories_attributes


  has_many   :council_histories


  has_many   :properties, :through => :council_histories, :foreign_key => :property_id
end

Council_history.rb - 加入表

class CouncilHistory < ActiveRecord::Base
   attr_accessible :council_id, :property_id, :vacant 

   belongs_to :council

   belongs_to :property
end

嵌套控制器

class Properties::BuildController < ApplicationController
  include Wicked::Wizard

   steps :council, :confirmed 

  def show
    @property = Property.find(params[:property_id])    
    @property.build_council_history do |council_history| 
    @council = council_history.build_council 
    end
    render_wizard
  end

查看表单

<%= simple_form_for @property, url: wizard_path, :method => 'put' do |f|  %>

  <%= f.simple_fields_for :council_history do |builder| %>
  <%= builder.input :property_id, as: :hidden, input_html: { value: @property.id } %> 
      <%= builder.input :council_id, :collection => Council.all  %>

      <%= builder.submit %>
    <% end %>
<% end %>

1 个答案:

答案 0 :(得分:0)

如果您将对未来提出其他问题,请务必避免在问题中使用authenticity_token ......

相关问题