Association and STI

314 views
Skip to first unread message

marek

unread,
Apr 26, 2012, 9:48:49 AM4/26/12
to factory_girl
Hi,

given following models:

class Company < ActiveRecord::Base
end

class User < ActiveRecord::Base
end

class Seller < Company
has_many :seller_users, :foreign_key => "company_id"
end

class SellerUser < User
belongs_to :seller, :class_name => "Company"
end

class Trader < Company
has_many :trader_users, :foreign_key => "company_id"
end

class TraderUser < User
belongs_to :trader, :class_name => "Company"
end

how can I create SellerUser factory?

My attempt:

FactoryGirl.define do
factory :seller, class: Seller do
name 'My seller'
end
end

FactoryGirl.define do
factory :seller_user, class: SellerUser do #|h|
email 'selle...@email.pl'
association :seller, factory: :seller
end
end

I get folowing exception:
ActiveModel::MissingAttributeError: can't write unknown attribute
`seller_id'
My seller_user doesn't have seller_id attribute, it has company_id
attribute.
Any way to override attribute name?

Hope it makes sense.

Thanks,
Marek

Joe Ferris

unread,
Apr 27, 2012, 10:12:27 AM4/27/12
to factor...@googlegroups.com
Hey Marek,

Are you able to instantiate your model without factory_girl? Try doing a SellerUser.create with the same attributes you're using in the factory.

Your factory looks fine, but you need to specify the foreign key in SellerUser if you aren't following the Rails convention:

    belongs_to :seller, :class_name => "Company", :foreign_key => "company_id"

My recommendation would be to follow the Rails convention and rename the column to "seller_id."

-Joe
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:

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
For more options, visit this group at

marek

unread,
Apr 27, 2012, 10:26:47 AM4/27/12
to factory_girl
Hi Joe,

thanks, I added :foreign_key => "company_id" to my SellerUser and
TraderUser as you suggested and it is fine now.

You are right, I should follow the convention.

Thank you.

Regards,
Marek
> > email 'seller_u...@email.pl (mailto:seller_u...@email.pl)'
> > association :seller, factory: :seller
> > end
> > end
>
> > I get folowing exception:
> > ActiveModel::MissingAttributeError: can't write unknown attribute
> > `seller_id'
> > My seller_user doesn't have seller_id attribute, it has company_id
> > attribute.
> > Any way to override attribute name?
>
> > Hope it makes sense.
>
> > Thanks,
> > Marek
>
> > --
> > 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 (mailto:factor...@googlegroups.com)
> > To unsubscribe from this group, send email to
> > factory_girl...@googlegroups.com (mailto:factory_girl...@googlegroups.com)
Reply all
Reply to author
Forward
0 new messages