【问题标题】:Padrino + FactoryGirl not performing Lazy LoadingPadrino + FactoryGirl 不执行延迟加载
【发布时间】: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


    【解决方案1】:

    对于那些可能感兴趣的人,这是我为解决此问题而采用的解决方案:

    在用户工厂中,改为:

    after(:create) do |user, evaluator|
      create_list(:rate, evaluator.rates_count, user: user)
    end
    

    做:

    after(:create) do |user, evaluator|
      user.rates << create_list(:rate, evaluator.rates_count, user: user)
    end
    

    这不是一个正确的解决方案,但现在解决了我的问题。

    干杯/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多