Polymorphic associations

653 views
Skip to first unread message

Thomas

unread,
Nov 18, 2009, 10:38:40 PM11/18/09
to factory_girl
It seems to have been asked before, but I can't find a definitive
answer.

I have a model, address:

class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end

with a factory:

Factory.define :address do |a|
a.line_one { Random.address_line_1 }
a.line_two { Random.address_line_2 }
...
end

I have another model, company:

class Company < ActiveRecord::Base
has_one :address, :as => "addressable", :dependent => :delete
end

Factory.define :company do |c|
c.name { Random.firstname + "'s
Company" }
c.email { Random.firstname.downcase +
"com...@example.com" }
c.association :address
...
end

The problem comes in using the company factory. I happen to have a not
null constraint in my database, so doing Factory(:company) raises this
exception:

>> Factory.build(:company)
ActiveRecord::StatementInvalid: Mysql::Error: Column
'addressable_type' cannot be null: INSERT INTO `addresses` (`city`,
`line_two`, `updated_at`, `updated_by_id`, `addressable_type`,
`zip_code`, `addressable_id`, `line_one`, `created_by_id`, `state`,
`created_at`) VALUES('Umnkomaaz', 'Fl 613', '2009-11-19 03:32:43',
NULL, NULL, '55000', NULL, '27452 Oak Rd', NULL, 'AR', '2009-11-19
03:32:43')

Factory Girl doesn't set the id or type field on the polymorphic
association. Factory.build(:company) fails similarly, factory girl
tries to save the address for some reason.

Anyone have any ideas? Thanks!

Andy

unread,
Nov 19, 2009, 10:48:31 PM11/19/09
to factory_girl
I wrote a simple post about Polymorphic associations in factory_girl,
it should help you somehow, see http://blog.wangyaodi.com/2009/11/03/polymorphic-association-in-factory_girl/

On Nov 19, 11:38 am, Thomas <tommyfis...@gmail.com> wrote:
> It seems to have been asked before, but I can't find a definitive
> answer.
>
> I have a model, address:
>
> class Address < ActiveRecord::Base
>   belongs_to :addressable, :polymorphic => true
> end
>
> with a factory:
>
> Factory.define :address do |a|
>   a.line_one              { Random.address_line_1 }
>   a.line_two              { Random.address_line_2 }
>   ...
> end
>
> I have another model, company:
>
> class Company < ActiveRecord::Base
>   has_one :address, :as => "addressable", :dependent => :delete
> end
>
> Factory.define :company do |c|
>   c.name                            { Random.firstname + "'s
> Company" }
>   c.email                           { Random.firstname.downcase +
> "comp...@example.com" }

Nathan Sutton

unread,
Nov 20, 2009, 9:32:30 AM11/20/09
to factor...@googlegroups.com
You could use an after_create callback to add the address.

For example:

Factory.define :company do |c|
c.name 'your mom inc'
c.email 'yourmo...@lolyourmom.com'
c.after_create {|company|
Factory(:address, :addressable => :company)
}
end
> --
> Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
> http://thoughtbot.com/services/training
>
> You received this message because you are subscribed to the "factory_girl" mailing list.
> To post to this group, send email to factor...@googlegroups.com
> To unsubscribe from this group, send email to
> factory_girl...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/factory_girl?hl=en

Thomas

unread,
Nov 21, 2009, 10:07:29 AM11/21/09
to factory_girl
Yeah, upgrading to FG 1.2.3 (I was at the previous version) and using
after_create works best.

Thanks!

On Nov 20, 9:32 am, Nathan Sutton <nathan.sut...@gmail.com> wrote:
> You could use an after_create callback to add the address.
>
> For example:
>
> Factory.define :company do |c|
>   c.name 'your mom inc'
>   c.email 'yourmomsme...@lolyourmom.com'
>   c.after_create {|company|
>     Factory(:address, :addressable => :company)
>   }
> end
>
> On Nov 19, 2009, at 9:48 PM, Andy wrote:
>
> > I wrote a simple post about Polymorphic associations in factory_girl,
> > it should help you somehow, seehttp://blog.wangyaodi.com/2009/11/03/polymorphic-association-in-facto...
Reply all
Reply to author
Forward
0 new messages