self-reference association

137 views
Skip to first unread message

Carlos Betancourt C.

unread,
May 12, 2013, 12:26:53 PM5/12/13
to factor...@googlegroups.com
Hi!
We have a comment model that can have many comments as childrens.
...
      has_many :childrens, class_name: self.to_s, foreign_key: :parent_id
      belongs_to :parent, class_name: self.to_s, foreign_key: :parent_id
...

In it's factory, we have a trait to handle this associations, so we can create a comment with many children comments.
...
    trait :with_childrens do
      after(:create) do |comment|
        create_list(:comment, parent: comment)
      end
    end    
...

But it a exception is raised: "FactoryGirl::AssociationDefinitionError: Self-referencing association 'parent' in 'comment'"

How can we treat this?

Regards,

Reply all
Reply to author
Forward
0 new messages