Ask A Question


You’re not receiving notifications from this thread.

LoadError: Unable to autoload constant Validators::LapseValidator

Fernando Flores asked in Databases

I wrote a custom validator for a model in my app, but when I try to access that model I get

LoadError: Unable to autoload constant Validators::LapseValidator, expected /home/lflores/Projects/cna-bdays/app/models/validators/lapse_validator.rb to define it 'load missing constant'

Here is a gist to the involved code:
What am I doing wrong? please help


It needs to be namespaced under Validators:

class Validators::LapseValidator < ActiveModel::Validator


Now I get this

TypeError: wrong argument type Class (expected Module)
from /home/lflores/Projects/cna-bdays/app/models/licencia.rb:2:in `include'

I'm confused...


You can't use include with a class. Remove that and reference the class directly like so:

class Licencia < ApplicationRecord
  validates_with Validators::LapseValidator

That should do the trick. 👍


Thank you much Chris, I may be wrong but this is the kind of detail I have not found in books like Obie Fernandez's 'The Rails 4 way', Hanson's 'Agile Web Development with Rails 5' nor Michael Hartl's 'Ruby on Rails Tutorial' which I have, not even in the Rails guides... What documentation or book do you recommend to get a deep knowledge on these details?


I spent a lot of time just reading source code for Rails and trying to apply those approaches to my own code.

Most of the deep knowledge is just Ruby, not Rails. I should mention, I made a course last year that digs into all these things that I wish I knew about Ruby when I was learning Rails. Explains a lot of approaches that you'll see in gems like Rails. Took me years to learn all this stuff, so I thought it'd be good to put it into a course. Might be worth checking out.


Wonderful, thank you =)

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 79,047+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.