【发布时间】:2015-06-12 00:26:46
【问题描述】:
我正在撰写关于 Padrino 的论文,使用 Active Record 作为 ORM 并使用 FactoryGirl 作为 Mocking 框架。
我正面临一种奇怪的行为。
我有两个模型:用户和速率。
- User has a 'has_many :rates' association;
- Rate has a 'belongs_to :user' association;
- 'rates' table has an integer attribute named 'user_id' (not created with 'references' on migration, but directly with 'integer').
我的关联运行良好,但仅在对父对象执行重新加载之后。
以下是与此问题相关的 sn-ps: https://bitbucket.org/snippets/cuscos/MbdAK
如果我启动“Padrino 控制台”并手动创建用户,这是当前行为:
$ user = FactoryGirl.create(:user_with_rates)
$ user.rates.length # Received '0', but expected '1'
$ user.rates.all.length # Received '1', OK
$ user.reload!
$ user.rates.length # Now I'm receiving '1' correctly
ActiveRecord 似乎没有出于任何原因执行延迟加载。
有人知道为什么会这样吗?
感谢迄今为止的所有支持。
【问题讨论】:
标签: ruby rspec factory-bot padrino