1

我只熟悉 Rspec,但现在我需要修复一个使用 Test::Unit 的库。

我发现当我重新加载记录时,关联变为 nil

例如:

test "accepts nested attributes from subscription to user ()" do
  @user = User.create(name:'Ash')
  @subscription = @user.create_subscription()
  puts "user: " << @user.inspect
  puts "subscription: " << @user.subscription.inspect

  @subscription.update_attributes({ :user_attributes => { :notification_comment => true } })
  @user.reload
  puts "user: " << @user.inspect
  puts "subscription: " << @user.subscription.inspect
  assert @user.subscription, "should have an subscription"
end

输出:

user: #<User id: 815823837, account_id: nil, name: "Ash", email: nil, settings: {}, created_at: "2013-02-07 04:28:51", updated_at: "2013-02-07 04:28:51">
subscription: #<Subscription id: 1, user_id: 815823837, created_at: "2013-02-07 04:28:51", updated_at: "2013-02-07 04:28:51">
user: #<User id: 815823837, account_id: nil, name: "Ash", email: nil, settings: {}, created_at: "2013-02-07 04:28:51", updated_at: "2013-02-07 04:28:51">
subscription: nil

为什么会这样?我做了检查,我可以确认我可以Subscription.find(@subscription.id),所以它保存在数据库中。

4

0 回答 0