users テーブルと user_details テーブルにデータを保存する以下のメソッドがあります。
@newUser 変数を EmailMailer に渡すと、user_details 属性にアクセスできません。データベースを再クエリせずに @newUser オブジェクトに user_details を渡すにはどうすればよいですか?
モデル
class User < ActiveRecord::Base
has_one :user_details, :dependent => :destroy
accepts_nested_attributes_for :user_details
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
end
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
end
コントローラ
# POST /users
def create
@newUser = User.new(params[:user], :include =>:user_details)
# create password
require 'securerandom'
password = SecureRandom.urlsafe_base64(8)
@newUser.password = password
respond_to do |format|
if @newUser.save
@newUser.build_user_details
# Tell the UserMailer to send a welcome Email after save
EmailMailer.welcome_email(@newUser).deliver
# To be used in dev only. Just tests if the email was queued for sending.
#assert ActionMailer::Base.deliveries.empty?
format.html {
flash[:success] = "User created successfully"
redirect_to(contacts_path)
}
else
format.html {
flash[:error] = flash[:error].to_a.concat resource.errors.full_messages
redirect_to(contacts_path)
}
end
end
end