Rails的new_record?如果事务回滚,则不重置(为true)

时间:2009-12-04 16:33:38

标签: ruby-on-rails ruby transactions

所以在Rspec中进行以下测试:

require 'spec_helper'
describe Author do
  self.use_transactional_fixtures = false

  after(:each) do
    Author.destroy_all
  end

  it "should behave normal when using transactions" do
    my_author = nil
    begin
      Author.transaction do
        my_author = Author.new(:name => "My Name")
        my_author.new_record?.should be_true
        my_author.save!
        raise "some exception"
      end
    rescue
    end
    Author.count.should == 0
    my_author.new_record?.should be_true
  end
end

最后一行:

my_author.new_record?.should be_true

给出:

'Author should behave normal when using transactions' FAILED
expected true, got false

至少当你回滚一些记录创建时,我希望它完全回滚,所以重置new_record?和id也是。我在这里错过了什么吗?这是有意的吗?我正在使用rails 2.3.5

2 个答案:

答案 0 :(得分:2)

Delph,这是rails中的一个错误。它应该重置id和new_record,但它不会。

请参阅以下链接以获取更多信息

https://rails.lighthouseapp.com/projects/8994/tickets/1948-transaction-block-sets-model-id-to-non-existent-row

答案 1 :(得分:0)

是的,这是有意的。事务操作是仅数据库块。它不会改变你的内存中的红宝石对象。