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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:36:17+00:00 2026-05-26T13:36:17+00:00

In a Rails 3.1 project, I have a model class with a custom validator:

  • 0

In a Rails 3.1 project, I have a model class with a custom validator:

class Car < ActiveRecord::Base
  validate :road_must_exist_nearby, :on => :create

  # ...

  def not_a_validator_method
    Road.exists_nearby?
  end

  def road_must_exist_nearby
    if !Road.exists_nearby?
      # ...
    end
  end
end

When I attempt to save my instance of Car …

> car = Car.new
> car.save

I get the following error:

NameError: uninitialized constant Car::Road

Why does calling Road.exists_nearby? from a normal instance method work?:

> car.not_a_validator_method
=> true

And why does calling it from a validator method raise an error, as though Rails believes Road should be called through Car?:

> car.road_must_exist_nearby
NameError: uninitialized constant Car::Road

And how can I make the validator method work?

  • 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-26T13:36:18+00:00Added an answer on May 26, 2026 at 1:36 pm

    I’m only guessing, but I’d say it’s a namespace/scope issue. Rails is interpreting the “Road” constant as existing in the scope of Car (ie Car::Road). You can probably get around it by referencing the global namespace using: “::Road”

    • 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 Project < ActiveRecord::Base has_many :project_members has_many :members, :through
In my Rails 3 project I have the following code for My controller: class
I have got an existing Rails project (Rails 2.3.8) and I got following Model
I'm using MetaSearch gem in my Rails 3 project. I have two models: class
In my Rails 3 project, I have a user model with a self referential
Please help with ActiveRecord testing. Trying my first Rails 3.1.0 project. There I have
In my rails 3.1 project I have a Book model that has a ID,
I have the following line of Rails 3 In the project.rb model this works
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
Starting a new rails project and we have a well-thought-out color palette, and want

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.