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

  • Home
  • SEARCH
  • 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 4079038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:45:00+00:00 2026-05-20T17:45:00+00:00

I have the following models: Users (id) Project (id, name) Roles (id, title) Permissions

  • 0

I have the following models:

Users (id)
Project (id, name)
Roles (id, title)
Permissions (user_id, role_id, project_id)

With Factory Girl I want to make a factory which includes 2 users, 1 project, and permissions associating the 2 users to the project.

Here’s what I have so far:

Factory.define :user do |user|
  user.fname                  "James"
  user.lname                  "Bond"
  user.email                  "james.bond@intuit.com"
  user.password               "password"
  user.password_confirmation  "password"
end

Factory.define :project do |project|
  project.name              "myproject"
  project.private_email     "myproject"
  project.user_id do
    (User.find_by_email('james.bond@intuit.com')).id
  end
  project.instance_id do
    (User.find_by_email('james.bond@intuit.com')).instance_id
  end

  project.permissions {|permissions| [permissions.association(:permission)] }

end


Factory.define :permission do |permission|
  permission.role_id         1
  permission.creator_id do
    (User.find_by_email('james.bond@intuit.com')).id
  end  
  permission.user_id do
    (User.find_by_email('james.bond@intuit.com')).id
  end
  permission.project_id do
    (Project.find_by_name('myProject')).id
  end


end

But this errors with:

Failure/Error: @project = Factory(:project)
RuntimeError:
Called id for nil, which would mistakenly be 4 — if you really wanted the id of nil, use object_id

I’m new to factory girl, am I doing this right? Thanks

  • 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-20T17:45:01+00:00Added an answer on May 20, 2026 at 5:45 pm

    You want to take advantage of the association method to associate models with another.

    You would do something like:

     project.user {|i| i.association(:user)}
    
     permission.creator {|i| i.association(:user)}
     permission.project {|i| i.association(:project)}
    

    etc.

    An example of this:

    Factory.define :user do |u|
      u.sequence(:email) { |n| "mike#{n}@awesome.com"}
      u.password "funnyguy1"
      u.password_confirmation {|u| u.password }
     end
    
    Factory.define :profile do |p|
      p.sequence(:username) { |n| "madmike#{n}"}
      p.user {|i| i.association(:user)}
    end
    

    You would do something like this, for example:

    Factory.define :permission do |permission|
      permission.role_id         1
      permission.creator {|i| i.association(:user)}
      permission.user {|i| i.association(:user)}
      permission.project {|i| i.association(:project)}
    end
    

    I would suggest reading the github page for FactoryGirl:

    https://github.com/thoughtbot/factory_girl

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

Sidebar

Related Questions

Let say I have the following desire, to simplify the IConvertible's to allow me
I have a project that adds elements to an AutoCad drawing. I noticed that
My question is about memory use and objects in actionscript 2. If I have
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
I am attempting to pull some information from my tnsnames file using regex. I
After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

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.