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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:11:07+00:00 2026-06-12T01:11:07+00:00

I am following ruby on rails tutorial . And I have few questions: 1.

  • 0

I am following ruby on rails tutorial .
And I have few questions:

1.Hier it is showed that

>> "a" * 51
=> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

so we try to compare if our user has a name of 51 “a”‘s and not of 51 charachters by typing this code:

before { @user.name = "a" * 51 }

It is showed as well that
(“a” * 51).length is 51.

so why dont we write
before { @user.name = (“a” * 51).length }
It is more logical, so that the user name consist of 51 charachters and not 51 a’s.

2.And as well this moment seems very strange. The explanation is as well odd.

    describe "when email address is already taken" do
        before do
  1.        user_with_same_email = @user.dup
  2.        user_with_same_email.save
        end

        it { should_not be_valid }
      end

in 1. we duplicate our original user (for example foo=User.new(name:”Piks”, email:”piks@piks.com”) and put the copie of the foo in the variable user_with_same_email.
in 2. we save the copie of the foo and it returns us “true”, so that we have saved the copie and the original foo has the email address that already exists in the database and threfore should be invalid.

But how can the original foo can be invalid it I want to save it? It is not logically at all to save the copie of the user and the original user is then invalid.

I guess I misunderstand the “before block”. The “before block”, namely the method user_with_same_email.save should return “false”. Why?Because for instance, we have our database full of emails(there are as well logins) and then I try to register myself with the email that already exists in the database.
So, the system makes a copy of me and save it in the user_with_same_email and try to save it, but it cannot because another user with my email already exists in the database. So, the method user_with_same_email.save will return false and hence I am considered to be an invalid user.

In this very example the “before block” should return “true” in roder to proceed to it { should_not be_valid } or am I wrong?

I will appreciate if somebody helps me.
Thank you

  • 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-12T01:11:09+00:00Added an answer on June 12, 2026 at 1:11 am

    You misunderstood the whole stuff. It is not the model code, it is a test for it. The tutorial tries to force test driven development. The idea is basically to specify statements about your system which are forced to be implemented, like “the name can’t be longer than 50 character”. You create a test for it where you try to break this statement, and if you can break it, then the system is faulty.

    Look it like this:

    before { @user.name = "a" * 51 }
    it { should_not be_valid }
    

    One test is to set the user’s name to a string more than 50 character long. This is guaranteed to be that long. And it should be invalid, as it is. The test system will notify you if you forgot to add the length constraint in your model.

            before do
      1.        user_with_same_email = @user.dup
      2.        user_with_same_email.save
            end
    
            it { should_not be_valid }
    

    This is just another test. It creates a dummy user, duplicates it, and tries to save both. This should fail as well. If it fails the test is successful and it means your code is good.

    One more thing, you asked why not just use before { @user.name = ("a" * 51).length }. This is clearly invalid, you just try to set the user’s name to be 51, a number, not a string 51 characters long.

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

Sidebar

Related Questions

I'm reading through the excellent Ruby on Rails Tutorial and have encountered the following
I've been following the exercises in the Ruby-on-Rails tutorial by M. Hartl. I have
I am following this routing tutorial for Ruby on Rails: http://guides.rubyonrails.org/routing.html It says that
I am new to Rails & Ruby and have been following the http://ruby.railstutorial.org/ruby-on-rails-tutorial-book and
I am currently following the Ruby on Rails Tutorial and have run into a
I am following along the Ruby on Rails Tutorial and have gotten somewhat confused
I'm following the Ruby on Rails tutorial and I'm on chapter 2. When I
I've been following the Ruby On Rails Tutorial and tried to get rid of
I found the following table structures while I was watching ruby on rails tutorial.
I'm following the Kevin Skoglund tutorial Ruby on Rails 3 Essential Training, which was

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.