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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:10:41+00:00 2026-05-23T01:10:41+00:00

What I have now: class User < ActiveRecord::Base has_many :people end … and… class

  • 0

What I have now:

class User < ActiveRecord::Base
   has_many :people
end

… and…

class Person < ActiveRecord::Base
   belongs_to :user
end

In spec/factories.rb:

Factory.define :user do |u|
  u.email "test@test.com"
  u.password "testpassword"
  u.password_confirmation "testpassword"
  u.display_name "neezer"
  # u.people { |i| [i.association(:person)] }
end

Factory.define :person do |p|
  p.first_name "p_firstname"
  p.last_name "p_lastname"
  p.gender "male"
  p.association :user
end

I want to setup the user factory to create with 1 person association, but if I uncomment that line, when I run my tests, my system hangs for quite some time, before outputting this failure:

1) User can be created from a factory
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/test/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.5/lib/active_record/persistence.rb:285

What am I doing wrong here? I would like to have tests that require an association between these two models, such that (1) a User must have at least 1 person, and (2) a Person must belong to a User.

Is this a first-priority issue? I’ll admit I’m a bit lost here…


I’m using rspec 2.5.0, factory_girl_rails 1.0.1, and rails 3.0.5.


My specs:

user_spec.rb:

require 'spec_helper'

describe User do

  subject { Factory :user }

  # ...

  context "has associations, " do

    it "can have people" do
      subject.should respond_to :people
    end

    it "must have at least 1 person" do
      subject.send "people=", nil
      subject.should_not be_valid
      subject.errors[:people].should_not be_empty
    end

  end

end

person_spec.rb:

require 'spec_helper'

describe Person do

  subject { Factory :person }

  # ...

  context "has validation, " do

    [:gender, :user].each do |attr|

      it "must have a #{ attr }" do
        subject.send "#{attr}=", nil
        subject.should_not be_valid
        subject.errors[attr].should_not be_empty
      end

    end

  end

  context "has associations, " do

    it "can have a User" do
      subject.should respond_to :user
    end

  end

end
  • 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-23T01:10:42+00:00Added an answer on May 23, 2026 at 1:10 am

    I’ve since discovered Shoulda, which provides a nice rspec matchers like these:

    subject.should belong_to :user
    subject.should have_many :people
    

    Which has solved my issue.

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

Sidebar

Related Questions

I have a one-to many relationship in Rails: class User < ActiveRecord::Base has_many :activities,
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
I have 2 Models: # user.rb class User < ActiveRecord::Base has_one :profile, :dependent =>
I have a Product model: class Product < ActiveRecord::Base belongs_to :subcategory define_index do #
Here's my User model: class User < ActiveRecord::Base has_many :friends, :class_name => 'Friendship', :dependent
so I have two models: class User < ActiveRecord::Base has_and_belongs_to_many :followed_courses, :class_name => Course
I have the points by the end of the GenerateButton class but now that
I have multiple classes that all derive from a base class, now some of
Modern ATL/MFC applications now have access to a new shared pointer class called CAutoPtr,
In Java, I have a subclass Vertex of the Java3D class Point3f . Now

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.