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!