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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:51:46+00:00 2026-05-26T08:51:46+00:00

context can have 2 companies associated with it(v2) do it should have an error

  • 0
  context "can have 2 companies associated with it(v2)" do
    it "should have an error when multiple companies can't belong to an industry " do
        @company1 = Factory(:company)
        @company2 = Factory(:company)
        @industry = Factory(:industry)
        @company1.industry = @industry
        @company2.industry = @industry
        @industry.should have(2).companies
    end
  end

This test is failing and I am having a hard time with it.
15 other tests are ok.
The problem is when I try to use related objects.

My models are:

class Company < ActiveRecord::Base
  belongs_to :industry
  validates_presence_of :name
  validates_length_of :state, :is => 2, :allow_blank => true
  validates_length_of :zip, :maximum => 30, :allow_blank => true
end
class Industry < ActiveRecord::Base
  has_many :companies
  validates_presence_of :name
  validates_uniqueness_of :name
  default_scope :order => "name asc"
end

Just inserting the records themselves does seem to work ok –

  context "can have 2 companies associated with it" do
    it "should have an error when multiple companies can't belong to an industry " do
      lambda do
        @company1 = Factory(:company)
        @company2 = Factory(:company)
        @industry = Factory(:industry)
        @company1.industry = @industry
        @company2.industry = @industry
      end.should change(Company, :count).by(2)
    end
  end

btw the top of my spec is:

require 'spec_helper'
describe Industry do
  before(:each) do
    @industry = Factory(:industry)
  end

I have also commented out

#  config.use_transactional_fixtures = true

at the bottom of spec/spec_helper.rb but that didn’t help

  • 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-26T08:51:46+00:00Added an answer on May 26, 2026 at 8:51 am

    If the company belongs to an industry, then when you create a company, it’s creating the industry for each.

    You’re accounting for this by setting the industry to the companies, but you’re not saving them. Alternatively, you can:

    before do
      @industry = Factory(:industry)
      @company1 = Factory(:company, :industry => @industry)
      @company2 = Factory(:company, :industry => @industry)
    end
    
    it "should have both companies" do
      @industry.companies.should == [@company1, @company2]
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can we have content text directly in <div>text content</div> or it should be like
context: I have an entity Book. A book can have one or more Descriptions.
I have an application which will be used by multiple companies.So my views/pages are
I have a map with multiple markers of companies using google maps API v3.
We have a hosted application that manages pages of content. Each page can have
I have a page where the user can edit various content using buttons and
I have a web app where people can create custom content on the web
How can I serve some static content in liferay? I have a directory structure
I have a form with a textarea that can contain large amounts of content
I'm looking content management system that would have features similar to stackoverflow: Users can

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.