Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

attribute_for and build ignored for relationships #68

Closed
pcastellazzi opened this issue Oct 10, 2011 · 3 comments
Closed

attribute_for and build ignored for relationships #68

pcastellazzi opened this issue Oct 10, 2011 · 3 comments
Assignees

Comments

@pcastellazzi
Copy link

if you have

Fabricator(:post) do
  comments(:count => 3)
end

and do Fabricate.build(:post) or Fabricate.attributes_for(:post). The comment object is always created instead of being built or hashed as expected.

@edlebert
Copy link

edlebert commented Nov 5, 2011

+1 pppppleeeeease!

@pdelanauze
Copy link

+1

I seem to only be affected using MongoId though... No problems with active record

In my case, I have ParentObject has_one ChildObject

The following example fails at assert_no_difference:

assert_difference 'ParentObject.count', 1 do
  parent_object = Fabricate(:parent_object)
  assert_difference "ParentObject.find('#{parent_object.id}').children_objects.count", 1 do
    assert_no_difference 'ParentObject.count' do
      built_child = Fabricate.build(:child_object)
    end
    parent_object.children_objects << built_child
  end
end

But this one works:

assert_difference 'ParentObject.count', 1 do
  parent_object = Fabricate(:parent_object)
  assert_difference "ParentObject.find('#{parent_object.id}').children_objects.count", 1 do
    parent_object.children_objects << Fabricate.build(:child_object, :parent_object => parent_object)
  end
end

@ghost ghost assigned paulelliott May 1, 2012
@paulelliott
Copy link
Owner

I am not convinced that cascading attributes_for is a good idea, so I only implemented it for builds.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants