Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:22:28+00:00 2026-05-30T21:22:28+00:00

I have this rails model association (has_many :through) class User < ActiveRecord::Base has_many :assignments

  • 0

I have this rails model association (has_many :through)

class User < ActiveRecord::Base
  has_many :assignments
  has_many :roles, :through => :assignments  
end

class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end

class Role < ActiveRecord::Base
  has_many :assignments
  has_many :users, :through => :assignments
end

I only have 3 roles (for now) in my application, each role :name is unique. So the role database contains 3 entries for now, user, admin, moderator.

How can I create factories so that I can create users, admins, and moderators and their roles ?

I would love to do something like:

(the role :name is checked for uniqueness, so it shouldn’t create 1 role entry per user, it should instead create 1 assignment to a role per user.)

# Create my roles
FactoryGirl.create(:role, :name => 'admin')  
FactoryGirl.create(:role, :name => 'user')
FactoryGirl.create(:role, :name => 'moderator')

# Create 10 users and 10 moderators assuming that the username is sequential or something
for i in 0..10
    user = FactoryGirl.create(:user) # user.roles.first.name.eql? 'user' #should be true
    mod = FactoryGirl.create(:moderator)  # mod.roles.first.name.eql? 'moderator' #should be true
end

How would you create the factories to model this association with FactoryGirl ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:22:29+00:00Added an answer on May 30, 2026 at 9:22 pm

    I contacted Josh Clayton (maintainer of Factory Girl) about this issue, he was kind to reply the following:

    The uniqueness constraint is where this will get you – sadly, you’ll have to create roles outside of the loop, name them and reuse those names. There may be other ways to hack around it, but I’m not really sure if and how you’d go about it.

    So in the end I solved the issue by doing something like:

         user_role = FactoryGirl.create(:role, :title => 'user')
    
         for i in (1..10) do
           user = FactoryGirl.create(:user)
           user.roles << user_role 
         end
    

    Just forgot about the “assignment” intermediate table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
We have a model association that looks something like this: class Example < ActiveRecord::Base
I have the following model: class Advisor < ActiveRecord::Base belongs_to :course end class Course
I'm trying to setup a simple has_many :through => association. I have 3 models.
I have a User and Group model. User has_many Groups and Group belongs_to User
I have a rails project where project model has many issues. class Project <
In my Rails 3 project, I have a user model with a self referential
I have a product model, a category model, and a product_categorization model (has_many..., through
Here's what I'm thinking: class Widget < ActiveRecord::Base has_one :widget_layout end class WidgetLayout <
I have this command in a Rails controller open(source) { |s| content = s.read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.