Rails 重定向不断从我的关联模型中删除数据



我有一个带有创建方法的表单,该方法输入的数据进入我的"用户"表和"user_details"表。

当我在创建方法完成后被重定向时,有一个"删除"查询可以从"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

从用户模型中注释掉after_initialize :build_user_details解决了我的问题。我添加了它,因为我遇到了这个问题 Rails 如何获取关联的模型属性,但这个问题现在也解决了。

最新更新