rails after_create 未更新父模型
rails after_create not updating parent model
使用从此解决方案获得的相同方法:
Use both Account and User tables with Devise
我正在尝试在保存用户后更新帐户模型上的 owner_id 列:
账户模型
class Account < ActiveRecord::Base
has_many :users
has_secure_token
accepts_nested_attributes_for :users
validates :name, :presence => true
end
用户模型
class User < ActiveRecord::Base
after_create :set_account_owner_id
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :account
validates :name, :presence => true
protected
def set_account_owner_id
self.account.owner_id = self.id
self.account.save
end
end
账户管理员
class AccountsController < ApplicationController
def new
# Create a new Account and then update owner_id with User profile id
@account = Account.new
@account.users.build
end
def create
@account = Account.new(account_params)
if @account.save
flash[:success] = "Account successfully created!"
redirect_to accounts_path
else
render 'new'
end
end
private
def account_params
params.require(:account).permit(:name, :token, :token_flag, :owner_id, users_attributes: [:name, :email, :password, :password_confirmation, :role])
end
end
Rspec 测试
RSpec.describe Account, type: :model do
it "should create an Account and a User through accepts_nested_attributes_for" do
@accountuser = {
:name => "My App Name",
:users_attributes => [{
:name => "John Doe",
:email => "jdoe@email.com",
:password => "password",
:password_confirmation => "password",
:role => "dev",
}]
}
account = Account.create!(@accountuser)
account.owner_id.should == account.users[0].id
end
end
测试失败并出现以下错误:
1) Account should create an Account and a User through accepts_nested_attributes_for
Failure/Error: account.owner_id.should == account.users[0].id
expected: 1
got: nil (using ==)
在阅读其他线程时,我认为问题是由于我没有在 after_create 中调用 'save',但是,我一直得到相同的结果。
如有任何见解,我们将不胜感激。
在 after_create
回调中,不保证您会有一个记录 ID。请改用 after_commit on: create
。
所以,不要调用 after_create :set_account_owner_id
,而是调用:
after_commit :set_account_owner_id, on: :create
此外,如果您使用 after_commit 测试代码,请记住添加 test_after_commit gem。来自 after_commit doc:
Note that transactional fixtures do not play well with this feature.
Please use the test_after_commit gem to have these hooks fired in
tests.
使用从此解决方案获得的相同方法:
Use both Account and User tables with Devise
我正在尝试在保存用户后更新帐户模型上的 owner_id 列:
账户模型
class Account < ActiveRecord::Base
has_many :users
has_secure_token
accepts_nested_attributes_for :users
validates :name, :presence => true
end
用户模型
class User < ActiveRecord::Base
after_create :set_account_owner_id
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :account
validates :name, :presence => true
protected
def set_account_owner_id
self.account.owner_id = self.id
self.account.save
end
end
账户管理员
class AccountsController < ApplicationController
def new
# Create a new Account and then update owner_id with User profile id
@account = Account.new
@account.users.build
end
def create
@account = Account.new(account_params)
if @account.save
flash[:success] = "Account successfully created!"
redirect_to accounts_path
else
render 'new'
end
end
private
def account_params
params.require(:account).permit(:name, :token, :token_flag, :owner_id, users_attributes: [:name, :email, :password, :password_confirmation, :role])
end
end
Rspec 测试
RSpec.describe Account, type: :model do
it "should create an Account and a User through accepts_nested_attributes_for" do
@accountuser = {
:name => "My App Name",
:users_attributes => [{
:name => "John Doe",
:email => "jdoe@email.com",
:password => "password",
:password_confirmation => "password",
:role => "dev",
}]
}
account = Account.create!(@accountuser)
account.owner_id.should == account.users[0].id
end
end
测试失败并出现以下错误:
1) Account should create an Account and a User through accepts_nested_attributes_for
Failure/Error: account.owner_id.should == account.users[0].id
expected: 1
got: nil (using ==)
在阅读其他线程时,我认为问题是由于我没有在 after_create 中调用 'save',但是,我一直得到相同的结果。
如有任何见解,我们将不胜感激。
在 after_create
回调中,不保证您会有一个记录 ID。请改用 after_commit on: create
。
所以,不要调用 after_create :set_account_owner_id
,而是调用:
after_commit :set_account_owner_id, on: :create
此外,如果您使用 after_commit 测试代码,请记住添加 test_after_commit gem。来自 after_commit doc:
Note that transactional fixtures do not play well with this feature. Please use the test_after_commit gem to have these hooks fired in tests.