I am trying to figure out how to write a factory that belongs to two different models, each of which must have the same parent model. Here's a contrived code example:
class User < ActiveRecord::Base has_many :widgets has_many :suppliers attr_accessible :username end class Widget < ActiveRecord::Base belongs_to :user has_many :parts attr_accessible :name end class Supplier < ActiveRecord::Base belongs_to :user has_many :parts attr_accessible :name end class Part < ActiveRecord::Base belongs_to :supplier belongs_to :widget attr_accessible :name end
Here is what I still have:
factory :user do name 'foo' end factory :widget do association :user name 'widget' end factory :supplier do association :user name 'supplier' end factory :part do association :widget association :supplier name 'part' end
The problem is that part.widget.user != part.supplier.user and they should be the same.
I tried the following without success:
factory :part do association :widget association :supplier, user: widget.user name 'part' end
Any suggestions? Or do I need to change it after creating the part?
thanks
source share