Unclear when to use a specific FactoryGirl syntax

factory-bot, ruby-on-rails-3

Solution

I learned the abbreviated notation is not supported in callbacks (such as after_create) as of the current version (3.2.0). This information came directly from the FactoryGirl teams via Google groups. I'll update this question when/if it's added in a future version.

Problem

In the latest release of FactoryGirl, some syntactic methods such as `Factory.create` were depreciated in favor of several others, most notably `FactoryGirl.create` and the simpler `create`. However, experience shows that certain syntaxes are not always appropriate given the context. Take for example: ``` FactoryGirl.define do factory :article do after_create {|a| a.comments << create(:comment) } end factory :comment do end end ``` Where Article has_many Comments, and Comments belongs_to Article. In the above factories, `a.comments << create(:comment)` issues the error `Comment(#nnn) expected, got FactoryGirl::Declaration::Static`. Change that line to `a.comments << FactoryGirl.create(:comment)` and the error goes away. It is not clear when one syntax should take precedence over any other form.

Original source