Rails new_record?事务内部的方法

时间:2015-06-27 14:38:53

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord transactions

我有一个表单对象,结构很难 - 一般来说,它包含在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 endednew_record?返回true(但task ready 存储在数据库中)。有没有办法检查事务块内的持久性?我想到的只是肮脏id.present?& valid?

1 个答案:

答案 0 :(得分:0)

由于new_record?&而我似乎错了。 persisted?在交易内部运作良好