2012-11-21 22 views
1

我有一個create方法的表單,該方法輸入進入我的「users」表和「user_details」表的數據。Rails重定向不斷刪除我的關聯模型中的數據

當create方法完成後我被重定向時,會出現一個「刪除」查詢,它從「user_details」表中刪除我的新記錄。

這個刪除行爲什麼會發生?

日誌示出了刪除發生

Redirected to http://localhost:3000/home 
Completed 302 Found in 518ms 


Started GET "/home" for 127.0.0.1 at 2012-11-20 20:32:25 -0600 
    Processing by SessionsController#index as HTML 
    SQL (0.2ms) BEGIN 
    (0.1ms) COMMIT 
    User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY id DESC LIMIT 5 
    UserDetails Load (0.6ms) SELECT `user_details`.* FROM `user_details` WHERE `user_details`.`user_id` = 34 LIMIT 1 
    SQL (0.1ms) BEGIN 
    SQL (0.2ms) DELETE FROM `user_details` WHERE `user_details`.`id` = ? [["id", 8]] 
    (6.0ms) COMMIT 
    UserDetails Load (0.5ms) SELECT `user_details`.* FROM `user_details` WHERE `user_details`.`user_id` = 1 LIMIT 1 
    SQL (0.1ms) BEGIN 
    (0.0ms) COMMIT 
    User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 
    CACHE (0.0ms) SELECT `user_details`.* FROM `user_details` WHERE `user_details`.`user_id` = 1 LIMIT 1 
    SQL (0.1ms) BEGIN 
    (0.1ms) COMMIT 
Rendered sessions/index.html.erb within layouts/application (0.2ms) 
Completed 200 OK in 235ms (Views: 63.6ms | ActiveRecord: 93.7ms) 

用戶模型

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login] 

    # Virtual attribute for authenticating by either username or email 
    # This is in addition to a real persisted field like 'username' 
    attr_accessor :login 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company, :user_details_attributes 

    has_one :user_details, :dependent => :destroy 
    accepts_nested_attributes_for :user_details 
    after_initialize :build_user_details 

    # validates email or username when logging in 
    def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) 
     where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first 
    else 
     where(conditions).first 
    end 
    end 

end 

User_details模型

class UserDetails < ActiveRecord::Base 
    belongs_to :user 

    attr_accessible :first_name, :last_name, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company 

    def full_name 
    [self.first_name, self.last_name].compact.join(' ') 
    end 

end 

會話控制器

class SessionsController < ApplicationController 
    layout 'application' 

    before_filter :authenticate_user! 

    def index 
     render :layout => 'application' 
    end 

    def new 
     render :layout => 'login' 
    end 
end 

應用控制器

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    before_filter :instantiate_user, :instantiate_message, :recent_users, :authenticate_user! 

    def instantiate_user 
     @user = User.new 
    end 

    def instantiate_message 
     @message = Message.new 
    end 

    def recent_users 
     @recents = User.last(5).reverse 
    end 
end 

回答