奇怪的 ActiveRecord::AssociationTypeMismatch [英] Strange ActiveRecord::AssociationTypeMismatch

查看:35
本文介绍了奇怪的 ActiveRecord::AssociationTypeMismatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行规范时遇到一个非常奇怪的错误:

I am getting a very strange error when running a spec:

Failure/Error: entity = Factory.create(:entity, :name => "Test Entity", :creator => user)
 ActiveRecord::AssociationTypeMismatch:
   ::User(#97318850) expected, got User(#92770800)

这是导致上述错误的代码.工厂是 factory_girl 工厂.

This is the code that results in the above error. Factory is a factory_girl factory.

  user = Factory(:user, :username => "kai", :email => "xxx@yyy.com", :password => "testing")
  entity = Factory.create(:entity, :name => "Test Entity", :creator => user)

当我使用 :creator =>User.first 然后一切都按预期工作.我打印了 User.firstuser,但看不出区别.

When I use :creator => User.first then everything works as expected. I printed out User.first and user, but see no difference.

有什么建议这里有什么问题吗?

Any suggestions what the heck is wrong here?

我在运行这个简单的请求规范时也遇到了这个错误

I also got this error when running this simple request spec

describe "Entities" do
  it "should succeed" do
    entity = Factory.create(:entity, :name => "Test Entity 1")
    visit root_path
  end

  it "should also succeed" do
    entity = Factory.create(:entity, :name => "Test Entity 2")
    property = Factory.create(:property, :entity => entity)
  end
end

这次我明白了

Failure/Error: property = Factory.create(:property, :entity => entity)
 ActiveRecord::AssociationTypeMismatch:
   Entity(#103620190) expected, got Entity(#96047070)

当我删除 visit root_path 时,一切正常(在单独运行每个规范时也是如此).这似乎是请求规范的问题.其他规格(模型、控制器)似乎运行良好.我使用 Capybara 1.0.0.beta1 和 RSpec 2.5.

when I delete visit root_path everything works fine (also when running each spec on its own). It just seems to be a problem for request specs. The other specs (model, controller) seems to run fine. I use Capybara 1.0.0.beta1 and RSpec 2.5.

类名后面的这个数字是什么意思?

What does this number behind the class name mean?

推荐答案

这是在加载了两个不同版本的模型时发生的错误.我曾经在旧版本的 Rails 3 中使用它,因为开发环境的模型重新加载器有点小故障.类名后面的数字代表类的不同版本.

This is an error that occurs when two different versions of the model have been loaded. I used to hit it in an older version of Rails 3, since the development environment's model reloader was slightly glitched. The numbers after the class name refer to different versions of the class.

这种错误在开发模式下可能会出现是合情合理的,但在测试模式下不应该出现,因为默认情况下,类是缓存的.查看 config/environments/test.rb 文件以确保 cache_classes 设置为 true.

It stands to reason that this sort of error might come up in development mode, but it shouldn't in test mode, because, by default, classes are cached. See the config/environments/test.rb file to ensure that cache_classes is set to true.

还要检查您使用的是最新版本的 Rails,3.0.7.这可能是一个已修复的错误.在此期间,请检查您是否使用 factory_girl 1.3.3.当完全正确地使用 API 时(我认为您正在这样做),剩下的唯一可能性是某些东西配置错误或者它是其他人代码中的错误.

Also check that you're on the latest version of Rails, 3.0.7. This may be a bug that has since been fixed. While we're at it, check that you're on factory_girl 1.3.3. When using the API totally correctly, which I think you're doing, the only possibilities left are that something is misconfigured or that it's a bug in someone else's code.

这篇关于奇怪的 ActiveRecord::AssociationTypeMismatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆