2012-08-07 54 views
0

我一直在寻找一段时间,似乎无法弄清楚是什么造成这种情况,我有一个模型工作节省志愿者和他的家庭和工作地址。我改变了一些东西(似乎无法弄清楚我的生活),现在地址没有被设置。错误的订单保存模型导轨

这是我的主要型号:

class HumanVolunteer < ActiveRecord::Base 

    has_one :primaryPhone, :class_name => "PhoneNumber", :foreign_key => "id", :primary_key => "phone_id" 
    has_one :primaryEmail, :class_name => "Email", :foreign_key => "id", :primary_key => "email_id" 
    has_one :work_adr, :class_name => "Address", :foreign_key => "id", :primary_key => "workaddressid" 
    has_one :home_adr, :class_name => "Address", :foreign_key => "id", :primary_key => "homeaddressid" 

    attr_accessible :firstName, :lastName, :homeaddressid, :notes, :status, :workaddressid, :home_adr, :work_adr, 
       :primaryPhone, :primaryEmail, :home_adr_attributes, :work_adr_attributes, :primaryPhone_attributes, 
       :primaryEmail_attributes 

    accepts_nested_attributes_for :home_adr 
    accepts_nested_attributes_for :work_adr 
    accepts_nested_attributes_for :primaryPhone 
    accepts_nested_attributes_for :primaryEmail 
end 

和地址型号:

class Address < ActiveRecord::Base 
    attr_accessible :city, :line1, :line2, :notes, :state, :zipcode 
    belongs_to :human_volunteer 
end 

继承人从human_volunteers_controller片段

# GET /human_volunteers/new 
    # GET /human_volunteers/new.json 
    def new 
    @human_volunteer = HumanVolunteer.new 
    @human_volunteer.build_home_adr 
    @human_volunteer.build_work_adr 
    @human_volunteer.build_primaryPhone 
    @human_volunteer.build_primaryEmail 

    # Set the Breadcrumbs for this page 
    @breadcrumbs = { "Dashboard" => "/", "Human Volunteers" => "/human_volunteers"} 
    @current_page = "New Volunteer" 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render :json => @human_volunteer } 
    end 
    end 

    # GET /human_volunteers/1/edit 
    def edit 
    @human_volunteer = HumanVolunteer.find(params[:id]) 
    if(@human_volunteer.home_adr.nil?) 
     @human_volunteer.build_home_adr 
    end 

    if(@human_volunteer.work_adr.nil?) 
     @human_volunteer.build_work_adr 
    end 

    if(@human_volunteer.primaryPhone.nil?) 
     @human_volunteer.build_primaryPhone 
    end 

    if(@human_volunteer.primaryEmail.nil?) 
     @human_volunteer.build_primaryEmail 
    end 

    @breadcrumbs = { "Dashboard" => "/", "Human Volunteers" => "/human_volunteers"} 
    @current_page = "Edit Volunteer: <em>" + @human_volunteer.firstName + " " + @human_volunteer.lastName + "</em>"; 
    end 

    # POST /human_volunteers 
    # POST /human_volunteers.json 
    def create 
    # create the volunteer 
    @human_volunteer = HumanVolunteer.new(params[:human_volunteer]) 

    respond_to do |format| 
     if @human_volunteer.save 
     format.html { redirect_to @human_volunteer, :notice => @human_volunteer.firstName + ' ' + @human_volunteer.lastName + ' was successfully created.' } 
     format.json { render :json => @human_volunteer, :status => :created, :location => @human_volunteer } 
     else 
     format.html { render :action => "new" } 
     format.json { render :json => @human_volunteer.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /human_volunteers/1 
    # PUT /human_volunteers/1.json 
    def update 
    @human_volunteer = HumanVolunteer.find(params[:id]) 

    respond_to do |format| 
     if @human_volunteer.update_attributes(params[:human_volunteer]) 
     format.html { redirect_to @human_volunteer, :notice => @human_volunteer.firstName + ' ' + @human_volunteer.lastName + ' was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render :action => "edit" } 
     format.json { render :json => @human_volunteer.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

我可以看到什么我认为这个问题通过查看日志,人类志愿者首先得到保存,使用地址码的空值s,然后地址被保存(也与电话号码和电子邮件模型相同的问题,它们与地址设置相同),然后地址被保存,然后人类志愿者不会再次用新值更新:

Parameters: {"authenticity_token"=>"dD+ut6noFPw8mJHq4rUJpuNBD1o+q0Hi8a+qOeetzMc=", "utf8"=>"✓", "human_volunteer"=>{"lastName"=>"Koch", "work_adr_attributes"=>{"line1"=>"", "line2"=>"", "zipcode"=>"", "state"=>"", "city"=>""}, "primaryEmail_attributes"=>{"email"=>""}, "primaryPhone_attributes"=>{"number"=>""}, "notes"=>"", "home_adr_attributes"=>{"line1"=>"123 Who Knows land", "line2"=>"", "zipcode"=>"11741", "state"=>"TS", "city"=>"testville"}, "firstName"=>"Ken", "status"=>"Investigative Candidate"}} 
    (0.2ms) BEGIN 
    (0.2ms) COMMIT 
    (0.2ms) BEGIN 
    (0.1ms) COMMIT 
    (0.1ms) BEGIN 
    (0.1ms) COMMIT 
    (0.1ms) BEGIN 
    (0.1ms) COMMIT 
    (0.1ms) BEGIN 
    SQL (2.3ms) INSERT INTO "human_volunteers" ("created_at", "email_id", "firstName", "homeaddressid", "lastName", "notes", "phone_id", "status", "updated_at", "workaddressid") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["email_id", nil], ["firstName", "Ken"], ["homeaddressid", nil], ["lastName", "Koch"], ["notes", ""], ["phone_id", nil], ["status", "Investigative Candidate"], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["workaddressid", nil]] 
    SQL (0.8ms) INSERT INTO "phone_numbers" ("created_at", "notes", "number", "phone_id", "phone_type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["notes", nil], ["number", ""], ["phone_id", nil], ["phone_type", nil], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00]] 
    SQL (0.7ms) INSERT INTO "emails" ("created_at", "email", "notes", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["email", ""], ["notes", nil], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00]] 
    SQL (0.9ms) INSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["city", ""], ["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["line1", ""], ["line2", ""], ["notes", nil], ["state", ""], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["zipcode", nil]] 
    SQL (0.5ms) INSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["city", "testville"], ["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["line1", "123 Who Knows land"], ["line2", ""], ["notes", nil], ["state", "TS"], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["zipcode", 11741]] 
    (17.8ms) COMMIT 

编辑:如果这是任何帮助这里是日志中的旧条目的例子:

Started POST "/human_volunteers" for 127.0.0.1 at Sat Jul 28 09:08:59 -0400 2012 
Processing by HumanVolunteersController#create as HTML 
    Parameters: {"human_volunteer"=>{"firstName"=>"Ken", "work_adr"=>{"city"=>"", "state"=>"", "zipcode"=>"", "line1"=>"", "line2"=>""}, "status"=>"Trainee", "home_adr"=>{"city"=>"fdsa", "state"=>"Nasdf", "zipcode"=>"11729", "line1"=>"asdf fdsafd", "line2"=>""}, "primaryEmail"=>"[email protected]", "primaryPhone"=>"6316813806", "lastName"=>"Koch", "notes"=>"\r\nHes a good guy"}, "authenticity_token"=>"bgbecE+nMxNrPtleNOLfO/MqRib0cXmDMqL5JaPyC10=", "utf8"=>"✓"} 
    [1m[36m (0.1ms)[0m [1mBEGIN[0m 
    [1m[35mSQL (0.5ms)[0m INSERT INTO "emails" ("created_at", "email", "notes", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["email", "[email protected]"], ["notes", nil], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00]] 
    [1m[36m (19.1ms)[0m [1mCOMMIT[0m 
    [1m[35m (0.1ms)[0m BEGIN 
    [1m[36mSQL (0.5ms)[0m [1mINSERT INTO "phone_numbers" ("created_at", "notes", "number", "phone_id", "phone_type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"[0m [["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["notes", nil], ["number", "6316813806"], ["phone_id", nil], ["phone_type", nil], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00]] 
    [1m[35m (56.9ms)[0m COMMIT 
    [1m[36m (0.1ms)[0m [1mBEGIN[0m 
    [1m[35mSQL (2.5ms)[0m INSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["city", ""], ["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["line1", ""], ["line2", ""], ["notes", nil], ["state", ""], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["zipcode", nil]] 
    [1m[36m (9.4ms)[0m [1mCOMMIT[0m 
    [1m[35m (0.1ms)[0m BEGIN 
    [1m[36mSQL (0.5ms)[0m [1mINSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"[0m [["city", "Deer Park"], ["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["line1", "294 West 15th St"], ["line2", ""], ["notes", nil], ["state", "NY"], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["zipcode", 11729]] 
    [1m[35m (7.0ms)[0m COMMIT 
    [1m[36m (0.1ms)[0m [1mBEGIN[0m 
    [1m[35mSQL (1.1ms)[0m INSERT INTO "human_volunteers" ("created_at", "email_id", "firstName", "homeaddressid", "lastName", "notes", "phone_id", "status", "updated_at", "workaddressid") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["email_id", 5], ["firstName", "Ken"], ["homeaddressid", 14], ["lastName", "Koch"], ["notes", "\r\nHes a good guy"], ["phone_id", 11], ["status", "Trainee"], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["workaddressid", 13]] 
    [1m[36m (6.1ms)[0m [1mCOMMIT[0m 
Redirected to http://localhost:3000/human_volunteers/1 
Completed 302 Found in 178ms (ActiveRecord: 112.1ms) 

我能回头到日志中,找到一个工作,而human_volunteer是最后一件事插入。它首先插入地址,电子邮件,电话,然后在插入志愿者时使用这些ID。

编辑:我也可以报告,我没有得到任何错误,一切都经过,只是数据没有连接。

任何想法可能导致订单的变化?我看不到我改变了任何影响这一点的事情。

更新:我试图改变HAS_ONE线和能够至少产生一个错误(更好或更差),当我cahnged行到:

has_one :workaddress, :class_name => "Address", :foreign_key => "id", :primary_key => "workaddress" 
    has_one :homeaddress, :class_name => "Address", :foreign_key => "id", :primary_key => "homeaddress" 

它尝试使用addressid 1每次我也得到

ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint "addresses_pkey" 
DETAIL: Key (id)=(1) already exists. 

另外,如果我拿出primary_key领域,似乎工作,但它试图将它们均设置为相同的地址,我也得到了同样的错误,除了与下一个ID(22,23 ,每次我尝试24次等)

也许这可以解释一些问题?我会继续刺激。

非常感谢你, - 肯

+0

你不能在你的提交历史看看,看看你修改了什么? – 2012-08-07 17:05:45

+0

我不确定导轨如何使用NameCasing破坏名称,但不应该* belongs_to:human_volunteer * be * belongs_to:humanvolunteer *? – 2012-08-07 17:09:14

+0

嘿谢谢你的回复,我试过使用:humanvolunteer,而不是运气。我还没有开始使用版本控制(只是在工作的早期阶段),所以我没有任何方式回滚。 – 2012-08-07 17:54:34

回答

0

我想出了什么似乎是一个可行的解决方案。

因为一旦ID最初设定,一切似乎都工作,我只是手动连接起来的控制器创建操作下:

@human_volunteer.homeaddress.save 
@human_volunteer.homeaddressid = @human_volunteer.homeaddress.id 
@human_volunteer.workaddress.save 
@human_volunteer.workaddressid = @human_volunteer.workaddress.id 

现在创建志愿者当地址被设置和一切否则应该很好地执行。

我能看到的唯一额外考虑因素是确保当我将验证器添加到地址时,地址可以在允许创建整个事件之前进行实际验证。

谢谢大家,

-Ken