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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:53:13+00:00 2026-05-23T17:53:13+00:00

Given I have the following class class listing > ActiveRecord::Base attr_accessible :address belongs_to :owner

  • 0

Given I have the following class

class listing > ActiveRecord::Base
  attr_accessible :address
  belongs_to :owner

  validates :owner_id, presence: true
  validates :address, presence: true
end

Is there a way I can get away with not having to keep associating an owner before I save a listing in my tests in /spec/models/listing_spec.rb, without making owner_id accessible through mass assignment?

describe Listing do
  before(:each) do
    @owner = Factory :owner
    @valid_attr = {
      address: 'An address',
    }
  end

  it "should create a new instance given valid attributes" do
    listing = Listing.new @valid_attr
    listing.owner = @owner
    listing.save!
  end

  it "should require an address" do
    listing = Listing.new @valid_attr.merge(:address => "")
    listing.owner = @owner
    listing.should_not be_valid
  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-23T17:53:14+00:00Added an answer on May 23, 2026 at 5:53 pm

    There is if you use factory-girl

      # it's probably not a good idea to use FG in the first one
      it "should create a new instance given valid attributes" do
        listing = Listing.new @valid_attr
        listing.owner = @owner
        listing.property_type = Factory(:property_type)
        listing.save!
      end
    
      it "should require an address" do
        # But here you can use it fine
        listing = Factory.build :listing, address: ''
        listing.should_not be_valid
      end
    
      it "should require a reasonable short address" do
        listing = Factory.build :listing, address: 'a'*245
        listing.should_not be_valid
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
I have the following models: class Campaign < ActiveRecord::Base has_many :campaign_keywords has_many :leads, :through
Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
I have the following tables: class SchoolClass(models.Model): id = models.AutoField(primary_key = True) class_name =
Given the following class structure: class Base { virtual void outputMessage() { cout <<
I have the following generic classes: class Base<T> where T : ... { ...
Given you have the following class (bad C#, but you get the drift): public
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
given i have the following block of code (function(){ var mb = { abc:function(){
I have the following problem: I am given a tree with N apples, for

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.