association problem

25 views
Skip to first unread message

Stefan Wintermeyer

unread,
Jan 27, 2011, 5:14:55 PM1/27/11
to factor...@googlegroups.com
Hi,

my situation:

manufacturer.rb:
# id :integer not null, primary key
# name :string(255)
has_many :ouis
has_many :phones

oui.rb
# id :integer not null, primary key
# value :string(255)
# manufacturer_id :integer
belongs_to :manufacturer

phone.rb
# id :integer not null, primary key
# mac_address :string(255)
# manufacturer_id :integer
belongs_to :manufacturer

The Oui stores the first 6 digists of the mac address in the field
value. A phone's mac address (stored in mac_address) has to start with
these 6 digits. I wrote a validation for that. The validation works
but I can't find out how I can create a factory for phone which
creates a manufacturer factory with a oui factory and than uses the
value field of that last factory to generate a random mac_address for
the phone.

Could someone tell me the magic to solve this?

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

Joe Ferris

unread,
Jan 28, 2011, 9:55:56 AM1/28/11
to factor...@googlegroups.com
Can you use an after_build hook to create a Oui with the first 6 digits of the mac address?

Factory.define :phone do |factory|
  factory.mac_address { Factory.next(:mac_address) }
  factory.after_build do |instance|
    Factory(:oui, :manufacturer => instance.manufacturer, :value => instance.mac_address.slice(0, 6))
  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,
Jan 28, 2011, 12:49:05 PM1/28/11
to factor...@googlegroups.com
On Fri, Jan 28, 2011 at 3:55 PM, Joe Ferris <jfe...@thoughtbot.com> wrote:
> Can you use an after_build hook to create a Oui with the first 6 digits of
> the mac address?
> Factory.define :phone do |factory|
>   factory.mac_address { Factory.next(:mac_address) }
>   factory.after_build do |instance|
>     Factory(:oui, :manufacturer => instance.manufacturer, :value =>
> instance.mac_address.slice(0, 6))
>   end
> end

Bingo!

Thank you so much.

Reply all
Reply to author
Forward
0 new messages