Rails使用外键设计奇怪的行为

时间:2012-11-03 11:59:41

标签: ruby-on-rails ruby-on-rails-3

我正在尝试创建一个简单的User&地址模型。

我创建了一个嵌套表单,其中的地址模型嵌套在User模型中。我的用户模型具有设计属性等。

一切正常,但创建用户时会发生奇怪的行为。

如果您在下面阅读,您会注意到“user_id”是首次提交记录时填充的拥有用户的地址。但随后紧接着,看起来像是Devise核心的东西,正在将user_id重置为null。

有谁知道会造成什么?

  

在2012-11-03 22:45:20 +1100开始发布“/ users”for 127.0.0.1   由Member :: UsersController处理#create as HTML参数:   { “UTF8”=> “中✓”,   “authenticity_token”=> “中Pr8lSEJalWwosuaLPOrYqQ + sOkFCyoNujaUybxpZDAg =”,   “用户”=> { “address_attributes”=> { “first_name的”=> “中华立”,   “last_name”=>“Hou”},“email”=>“HollyHou@hisense.com.au”,   “password”=>“[FILTERED]”,“password_confirmation”=>“[FILTERED]”}}   创建新用户(0.1ms)BEGIN(0.1ms)COMMIT(0.0ms)   BEGIN会员::用户存在(0.3ms)SELECT 1 AS FROM FROM   member_users WHERE member_usersemail = BINARY   'hollyhou@hisense.com.au'LIMIT 1 SQL(0.2ms)INSERT INTO   member_userscreated_atcurrent_sign_in_at,   current_sign_in_ipemailencrypted_password,   last_sign_in_atlast_sign_in_ipremember_created_at,   reset_password_sent_atreset_password_tokensign_in_count,   updated_at)VALUES('2012-11-03 11:45:20',NULL,NULL,   'hollyhou@hisense.com.au',   '$ 2a $ 10 $ 1JvC / sO85L2esNp8xA9qseTc78.rtOyF3zSBi / fOTiWJRt9.zgYha',NULL,   NULL,NULL,NULL,NULL,0,'2012-11-03 11:45:20')SQL(0.2ms)   INSERT INTO member_addressesaddress3address_1address_2,   address_typebusiness_namecitycountrycreated_at,   first_namegeo_lategeo_longis_primarylast_name,   middle_namephone1phone1_typephone2phone2_type,   postcodeprimary_emailsecondary_emailstatestatus,   suburbtitleupdated_atuser_id)VALUES(NULL,NULL,NULL,   NULL,NULL,NULL,NULL,'2012-11-03 11:45:20','HOlley',NULL,NULL,   NULL,'Hou',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,   NULL,NULL,NULL,'2012-11-03 11:45:20',5)(1.3ms)COMMIT
  (0.1ms)BEGIN(0.2ms)UPDATE member_users SET last_sign_in_at   ='2012-11-03 11:45:20',current_sign_in_at ='2012-11-03 11:45:20',last_sign_in_ip ='127.0.0.1',current_sign_in_ip ='127.0.0.1',   sign_in_count = 1,updated_at ='2012-11-03 11:45:20'在哪里   member_usersid = 5(0.2ms)COMMIT(0.1ms)BEGIN
  (0.2ms)UPDATE member_addresses SET user_id = NULL,updated_at   ='2012-11-03 11:45:20'WHER member_addressesid = 5(0.2ms)COMMIT

控制器是一个自定义控制器,因为我最终(一旦我开始工作),需要能够在注册后重定向到2个不同的位置,具体取决于客户登录的位置(在这种情况下是checkout或header)。

class Member::UsersController < Devise::RegistrationsController
  def create
    super
  end

  def new
    super
  end

  def edit
    Rails.logger.debug { "Editing the user" }
    super
  end

  def show
    Rails.logger.debug { "Showing the user controller" }
  end
end

和来自routes.rb

  devise_for :users, :class_name => "Member::User", :controllers => {:registrations => 'member/users', :sessions => 'member/sessions'}

1 个答案:

答案 0 :(得分:0)

我终于弄明白了。它实际上与我之前关于嵌套表单的问题得到的答案有关。

在下面的代码中,之前在嵌套地址字段中,我总是调用build_address,因为当表单刷新时,地址是一对一的关系,它创建了一个新地址。现在我认为它应该没问题,我还没有测试过,但至少解决了地址无效问题。

    resource_name,:url =&gt;   registration_path(resource_name),: html =&gt; {:class =&gt;   'form-horizo​​ntal'})do | f | %GT;               必填字段标有*

               “alert alert-error”,:id =&gt; “flash_error”)如果flash [:error]%&gt;                “flash_notice”)如果flash [:notice]%&gt;                “alert alert-error”,:id =&gt; “flash_alert”)如果flash [:alert]%&gt;                                                                                    {:class =&gt; “input-xlarge”}%&gt;                    {:class =&gt; “input-xlarge”}%&gt;               
        <%= f.input :email, :input_html => {:class => "input-xlarge"} %>
        <%= f.input :password, :required => true, :input_html => {:class => "input-xlarge"}   %>
        <%= f.input :password_confirmation, :required => true, :input_html => {:class => "input-xlarge"} %>
        <div class="form-actions">
          <%= f.button :submit, "Register", :class => "btn btn-success btn-large" %>
        </div>
    <% end %>