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 4330634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:59:22+00:00 2026-05-21T09:59:22+00:00

I have 2 models, User and Bucket . User has_many Buckets and a Bucket

  • 0

I have 2 models, User and Bucket. User has_many Buckets and a Bucket belongs_to a User.

In factories.rb, I have:

Factory.define :user do |user|
  user.email  "teste@test.com"
  user.password               "foobar"
  user.password_confirmation  "foobar"
end


Factory.sequence :email do |n| 
  "person-#{n}@example.com"
end

Factory.define :bucket do |bucket|
  bucket.email        "user@example.com"
  bucket.confirmation false
  bucket.association :user
end

and I have a login_user module as follows:

def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = Factory.create(:user)
      #@user.confirm!
      sign_in @user
    end
  end

I am using Spork and Watch and my Buckets_controller_spec.rb is as simple as:

describe "User authenticated: " do

   login_user  

   @bucket = Factory(:bucket)

   it "should get index" do
     get 'index'
     response.should be_success
   end
...
end

The error is always the same:

Failures:

  1) BucketsController User authenticated: should get index
     Failure/Error: Unable to find matching line from backtrace
     ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken
     # ./lib/controller_macros.rb:12:in `block in login_user'

And it only happens when I have the Factory(:bucket). The login works fine when I don’t add the Factory(:bucket).

It’s always the same error. I have tried adding :email => Factory.next(:email) to the user, but no success.

Edit:

In rails c test:

ruby-1.9.2-p180 :019 > bucket = Factory(:bucket, :email => "hello@hello.com")
    ActiveRecord::RecordInvalid: Validation failed: Email has already been taken

    ruby-1.9.2-p180 :018 >   Bucket.create(:email => "hello@hello.com")
     => #<Bucket id: 2, email: "hello@hello.com", confirmation: nil, created_at: "2011-04-08 21:59:12", updated_at: "2011-04-08 21:59:12", user_id: nil> 

Edit 2:

I found out that the error is in the association, however, I don’t know how to fix it.

  bucket.association :user
  • 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-21T09:59:23+00:00Added an answer on May 21, 2026 at 9:59 am

    When you define a factory with an association you need to give the factory an object to associate with whenever you use the factory.

    This should work:

    describe "User authenticated: " do
      login_user
      @bucket = Factory(:bucket, :user => @user)
    
      it "should get index" do
        get 'index'
        response.should be_success
      end
    end
    

    That way factorygirl knows to make a bucket which is associated with @user.

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

Sidebar

Related Questions

I have 3 models class User < ... belongs_to :language has_many :posts end class
I have 3 models: User has_many :questions has_many :corrections end Question has_one :correction belongs_to
I have two models: User has_many :prices Price belongs_to :users I want to show
I have 3 models: User: has_many :comments Video: has_many :comments Comment: belongs_to :video, :user
I have two Models called User and Membership. User has_many :memberships Membership belongs_to :user
I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password
I have 2 models User and Address . class User < ActiveRecord::Base has_many :addresses
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have three models: user, subscription, and channel. User: has_many :channels, :through => :subscriptions

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.