I'm stuck with the following scenario (the database layout is fixed I
can't change it):
sip_domain.rb
has_many :tenants
tenant.rb
belongs_to :sip_domain
has_many :users
user.rb
belongs_to :tenant
has_many :sip_accounts
validate_presence_of :tenant
sip_account.rb
belongs_to :sip_domain
belongs_to :user
belongs_to :tenant
validate_presence_of :sip_domain, :user, :tenant
I'm searching for a way to define a factory for sip_account. I have
factories for all the other models but sip_account is a tough one.
If I do something like:
Factory.define :sip_account do |f|
f.association :sip_accountable, :factory => :user
f.sequence(:auth_name) {|n| "auth_name#{n}" }
f.sequence(:caller_name) {|n| "Foo Account #{n}" }
f.sequence(:password) {|n| "123" }
f.association :tenant
f.association :sip_domain
end
Than the user doesn't belong to the tenant and the tenant doesn't
belong to the sip_domain. And that breaks the validations which is a
bit more complicated that the example above.
How can I let the different associations interact with each other?
Stefan
--
AMOOMA GmbH - Bachstr. 124 - 56566 Neuwied --> http://www.amooma.de
Geschäftsführer: Stefan Wintermeyer, Handelsregister Montabaur B14998
Bücher: http://das-asterisk-buch.de - http://ruby-auf-schienen.de
--
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
On Fri, Dec 9, 2011 at 8:12 PM, Duncan Beevers <dun...@dweebd.com> wrote:
> after_build do |sip_account|
> sip_account.tenant ||= FactoryGirl.build :tenant, sip_domain: sip_domain
> sip_account.sip_accountable ||= FactoryGirl.build :user, tenant:
> sip_account.tenant
> end
Thanks for the advice with after_build. That solved the problem.