Associations interact with each other

44 views
Skip to first unread message

Stefan Wintermeyer

unread,
Dec 9, 2011, 1:44:20 PM12/9/11
to factor...@googlegroups.com
Hi,

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

Duncan Beevers

unread,
Dec 9, 2011, 2:12:52 PM12/9/11
to factor...@googlegroups.com
I think you can deal with this using an after_build block.

FactoryGirl.define :sip_account do
  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
  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
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

Stefan Wintermeyer

unread,
Dec 10, 2011, 12:02:39 PM12/10/11
to factor...@googlegroups.com
Hi,

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.

Reply all
Reply to author
Forward
0 new messages