On Dec 13, 4:05 pm, David Kahn <d...@structuralartistry.com> wrote:
> @person = Factory(:person)
> @person.addresses.size.should == 0
> address = Factory(:address)
> @person.add_address(address)
> @person = Person.find(@person.id) # if I dont do this I get the old value,
> 0, but if I reload I get the right value, 1.
> @person.addresses.size.should == 1
> @person.addresses[0].person_id.should == @person.id
> Factory.define :person do |f|
> f.first_name {"MyString"}
> f.middle_name {"MyString"}
> f.last_name {"MyString"}
> end
> Factory.define :address do |f|
> f.person_id {1}
> f.address {"MyString"}
> f.zip {"94587"}
> f.city {"MyString"}
> f.state {"MyString"}
> end
Side note about FG usage: I was under the impression that this is
suboptimal for FG because now Factory(:address) generates and invalid
object unless you do something special. I traditionally write this as
like this:
Factory.define :address do |f|
f.association :person
f.address {"MyString"}
f.zip {"94587"}
f.city {"MyString"}
f.state {"MyString"}
end
This way if I want an address to test and I don't care about it's
person, then Factory(:address) gives me a valid item. And then if I
do care about the association then we do:
@person = Factory(:person)
@person.addresses.size.should == 0
@address = Factory(:address, :person => @person)
@person.address.size.should == 0
@person.reload.address.size.should == 1
Keep in mind that in this case you don't want to do:
@person = Factory(:person)
@address = Factory(:address)
@person.addresses << @address
Because the second line will generate another, unneeded Person object.
\Peter
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment