Railsnew_record?事务内部方法
Rails new_record? method inside transaction
我有一个表单对象,它的结构很复杂 - 通常,它包含在 ActiveRecord::Base.transaction
块中:
ActiveRecord::Base.transaction do
task = Task.create(valid_params)
puts task # => Task id: 1, title: 'abc'... id is present, so, the task in the database
do_something unless task.new_record? # do something if the task in the database
end
看起来很奇怪,但是因为transaction wasn't ended,new_record?
returns true (but task
is ready be存储在数据库中)。有没有办法检查事务块内的持久性?我想到的只是脏 id.present?
& valid?
.
看来我错了,因为 new_record?
& persisted?
在事务中运行良好
我有一个表单对象,它的结构很复杂 - 通常,它包含在 ActiveRecord::Base.transaction
块中:
ActiveRecord::Base.transaction do
task = Task.create(valid_params)
puts task # => Task id: 1, title: 'abc'... id is present, so, the task in the database
do_something unless task.new_record? # do something if the task in the database
end
看起来很奇怪,但是因为transaction wasn't ended,new_record?
returns true (but task
is ready be存储在数据库中)。有没有办法检查事务块内的持久性?我想到的只是脏 id.present?
& valid?
.
看来我错了,因为 new_record?
& persisted?
在事务中运行良好