1

「users」テーブルと「user_details」テーブルに入るデータを入力する create メソッドを含むフォームがあります。

create メソッドの完了後にリダイレクトされると、"user_details" テーブルから新しいレコードを削除する "delete" クエリがあります。

この削除行が発生するのはなぜですか?

削除の発生を示すログ

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
4

1 に答える 1

1

after_initialize :build_user_detailsユーザーモデルからコメントアウトすると、問題が解決しました。この問題Rails how to get associated model attributesがあったので追加しましたが、その問題も解決されました。

于 2012-11-21T02:47:35.477 に答える