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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:49:05+00:00 2026-06-07T07:49:05+00:00

I have a profile model like so; class Profile < ActiveRecord::Base attr_accessible :first_name, :last_name

  • 0

I have a profile model like so;

class Profile < ActiveRecord::Base
  attr_accessible :first_name, :last_name
  belongs_to :user
  validates :user, presence: true
  validates :first_name, presence: true, on: :update
  validates :last_name, presence: true, on: :update
end

I am wanting to write some rspec tests to test the validation of first_name and last_name but I cannot see how to run profile.should_not be_valid only on update in a model test. Something like;

it "should be invalid without a first name on update" do
  profile = FactoryGirl.build :profile
  profile.first_name = nil
  profile_should_not be_valid
end

doesn’t differentiate between an update or create action and I can’t see anything in the rspec documentation about this. Surely it’s a fairly common thing to test.

  • 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-06-07T07:49:06+00:00Added an answer on June 7, 2026 at 7:49 am

    be_valid in rspec just calls valid? on the model.

    profile = FactoryGirl.build :profile
    

    This builds a new model instance for Profile, but does not commit it to the database. You would use this profile for your create test. Setting :first_name to nil should and calling profile.should be_valid should pass.

    profile = FactoryGirl.create :profile
    

    This builds and inserts the model instance for Profile into the database. You would use this profile for your update test. Setting :first_name to nil should and calling profile.should be_valid should fail.

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

Sidebar

Related Questions

I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I have a simple model like class Interest < ActiveRecord::Base has_and_belongs_to_many :user_profiles end class
I have a Profile model: class Profile(db.Model): user = db.UserProperty(auto_current_user=True) bio = db.StringProperty() I'd
I have: class Profile(models.Model): user = models.OneToOneField(User) birthday = models.DateField(null=True) gender = models.CharField(max_length=140) profile_picture
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I would like to access profiles table from my Profile model which belongs_to :user
Here are my models: class BillingProfile < ActiveRecord::Base belongs_to :student attr_accessible :cost end class
I have multiple models that point to the same user like this: class Model1(moldels.Model):
15 class Profile(models.Model): 16 17 User profile model 18 19 user = models.ForeignKey(User, unique=True)
I have a django model like so: class Profile_Tag(models.Model): profile = models.ForeignKey(Profile) tag =

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.