Associations creating duplicates in create! operation

117 views
Skip to first unread message

Martin Streicher

unread,
Jul 28, 2009, 6:52:39 AM7/28/09
to factory_girl
What is the proper technique to write a factory that contains
associations to other models who also have factories? Do you do...

x.association :other_model
x.association OtherModel.new
x.association Factory :other_model

Something else?

How do you avoid ...

ActiveRecord::RecordInvalid in 'Message should create a new instance
given valid attributes'
Validation failed: Url has already been taken, Provider has already
been taken

... where Message has an association to Account which has an
association to Provider which validates the uniqueness of two fields.
provider and url?

Martin

Martin Streicher

unread,
Jul 28, 2009, 7:17:04 AM7/28/09
to factory_girl
And a related question...

How do I avoid errors like this...

/Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.2/lib/
factory_girl/factory.rb:299:in `factory_by_name': No such factory:
provider (ArgumentError)

This occurs when I try...

Factory.define :em do |e|
e.association Factory.build(:account)
end

... again, when Account says something like ...

Factory.define :account do |a|
a.association :provider
end

Gravis

unread,
Jul 30, 2009, 5:22:45 PM7/30/09
to factory_girl
On Jul 28, 12:52 pm, Martin Streicher <martin.streic...@gmail.com>
wrote:

> ActiveRecord::RecordInvalid in 'Message should create a new instance
> given valid attributes'
> Validation failed: Url hasalreadybeentaken, Provider hasalready
> beentaken

I'm not sure it's related, but I have a strange issue :

this simple factory will generate a "ActiveRecord::RecordInvalid:
Validation failed: Item has already been taken" :

Factory.define :ownership do |o|
o.association :item
o.association :user
end

only with tests (works fine in dev env in console).
If I remove the "validates_uniqueness_of :item_id, :scope => :user_id"
validation from the model, error goes away.

Gravis

unread,
Jul 30, 2009, 5:25:17 PM7/30/09
to factory_girl
Sounds like we have a bug here, or I'm missing something.

I solve my problem by doing :

@ownership = Factory.build(:ownership)
@ownership.save

instead of

@ownership = Factory(:ownership)

which should be exactly the same ?

I'll open a ticket if someone can confirm.

thanks

JDS

unread,
Aug 2, 2009, 12:02:49 PM8/2/09
to factory_girl
I'm having the same problem. It looks like your factory definitions
will create all the records in the database before you start. Then
when you come along and do Factory.create(:my_thingy) if the object
has a unique constraint it'll get an error because FG tries to create
a new object ready for writing to the DB, but there's another one
already there.

If FG is going to create all the records defined in the factory
definitions before your tests start then it really needs Factory.read
(:etc) to complement Factory.build and Factory.create.

It's not much use creating associations either.

I'm not sure if this is a feature, a bug or whatever. It does mean
that I can't use FG for useful work.

Cheers

John Small

Martin Streicher

unread,
Aug 2, 2009, 12:25:30 PM8/2/09
to factor...@googlegroups.com

I switched to machinist and am very pleased with it. I especially like
the Sham and Faker integration to build records to suit.

JDS

unread,
Aug 2, 2009, 12:37:56 PM8/2/09
to factory_girl


On Aug 2, 5:02 pm, JDS <jds...@gmail.com> wrote:
> I'm having the same problem. It looks like your factory definitions
> will create all the records in the database before you start.

OK, I'll hang my head in shame. I've sussed my problem, and I could be
the same problem other people are having. I didn't delete my fixtures
yaml file after switching to creating records via FG. So of course the
fixtures were getting loaded before the tests, so when I then did
Factory.create I'd get a problem because I've copied stuff in from the
original yaml file and the unique columns will have duplicate values.

So if other people are new to FG and are having problems with
duplicates on unique values, check you've deleted your yaml fixtures.

Cheers

John Small

Mark Wilden

unread,
Aug 2, 2009, 3:23:45 PM8/2/09
to factor...@googlegroups.com
On Sun, Aug 2, 2009 at 9:37 AM, JDS<jds...@gmail.com> wrote:
>
> OK, I'll hang my head in shame. I've sussed my problem, and I could be
> the same problem other people are having. I didn't delete my fixtures
> yaml file after switching to creating records via FG

Thanks for reporting back, instead of just skulking off. :)

///ark

Philippe Lafoucrière

unread,
Aug 2, 2009, 3:50:29 PM8/2/09
to factor...@googlegroups.com
OMG...

can't believe I forgot to check that. Fixtures are created by the scaffold generator, Dho !

Philippe Lafoucrière

unread,
Aug 3, 2009, 4:45:55 AM8/3/09
to factor...@googlegroups.com


2009/8/2 Philippe Lafoucrière <philippe.l...@gmail.com>

OMG...

can't believe I forgot to check that. Fixtures are created by the scaffold generator, Dho !


not really related, but I've spent almost 2 hours figuring why my factory generated an error when a has_many relationship was added to my model.
It turns out "binding" is a reserved word, and can't be used as a column name ...

Mark Wilden

unread,
Aug 3, 2009, 10:30:31 AM8/3/09
to factor...@googlegroups.com
2009/8/3 Philippe Lafoucrière <philippe.l...@gmail.com>:

> It turns out "binding" is a reserved word, and can't be used as a column
> name ...

To be completely nit-picky, binding is a method on Object.

///ark

Reply all
Reply to author
Forward
0 new messages