Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcoslebron/8bdff6fbeffbb8e3f689fb15b250c0ac to your computer and use it in GitHub Desktop.
Save marcoslebron/8bdff6fbeffbb8e3f689fb15b250c0ac to your computer and use it in GitHub Desktop.
Has Many through factory with fabricator
class Country < ActiveRecord::Base
has_many :registrations
has_many :members
has_many :distribution_groups, :through => :members
end
class DistributionGroup < ActiveRecord::Base
has_many :members
has_many :countries, :through => :members
end
class Member < ActiveRecord::Base
belongs_to :country
belongs_to :distribution_group
end
Fabricator(:country) do
name { Faker::Address.country }
end
Fabricator(:member) do
end
Fabricator(:distribution_group) do
name { Faker::Lorem.words(3).join(" ") }
after_build do |distribution_group|
2.times { Fabricate(:member, :distribution_group => distribution_group, :country => Fabricate(:country)) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment