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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:36:27+00:00 2026-06-11T09:36:27+00:00

My Location model which can have a website. The website only has to be

  • 0

My Location model which can have a website. The website only has to be present if the location is online. Before it is saved, the website must have the correct format.

location.rb

class Location < ActiveRecord::Base
  attr_accessible :online :website
  validates_presence_of :website, :if => 'online.present?'
  validates_inclusion_of :online, :in => [true, false]
  validate :website_has_correct_format, :if => 'website.present?'

  private

  def website_has_correct_format
    unless self.website.blank?
      unless self.website.downcase.start_with?('https://', 'http://')
        errors.add(:website, 'The website must be in its full format.)
      end
    end
  end
end

I make a spec to test for this:

location_spec.rb

require 'spec_helper'

describe Location do

  before(:each) do
    @location = FactoryGirl.build(:location)
  end

  subject { @location }

  describe 'when website is not present but store is online' do
    before { @location.website = '' && @location.online = true }
    it { should_not be_valid }
  end
end

The test fails, bringing to me the error:

Failures:

  1) Location when website is not present but store is online
     Failure/Error: it { should_not be_valid }
     NoMethodError:
       undefined method `downcase' for true:TrueClass
     #./app/models/location.rb:82:in `website_has_correct_format'
     #./spec/models/location_spec.rb:72:in `block (3 levels) in <top (required)>'

What is the solution to this problem?

  • 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-11T09:36:28+00:00Added an answer on June 11, 2026 at 9:36 am

    Your spec file is written a little bit wrong. The && is not working the way you expect it to.

    require 'spec_helper'
    
    describe Location do
    
      before(:each) do
        @location = FactoryGirl.build(:location)
      end
    
      subject { @location }
    
      describe 'when website is not present but store is online' do
        before do
          @location.website = ''
          @location.online = true
        end
    
        it { should_not be_valid }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model with a location, which itself has a latitude and longitude. What
I have model show which has_many performances and a performance has a certain location.
I have a Job model which can have many attachments. The Attachment model has
I have a model location, which has many courses If I destroy a location,
I have a model for Location which has a title, description, and type. The
I have a model of events that has various information such as date, location,
A given location (city), can have a name and also other aliases by which
I have a model like this: Client - which has many Locations - which
I have written a checkers game model which has a Board object that holds
I have a CoreData model with an object bar which has a to-many relationship

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.