Ask A Question

Notifications

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: https://gist.github.com/lflores1961/8ad21b6e903cd4e2028133087ea23f08
What am I doing wrong? please help

Reply

It needs to be namespaced under Validators:

class Validators::LapseValidator < ActiveModel::Validator

Reply

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...

Reply

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

class Licencia < ApplicationRecord
  validates_with Validators::LapseValidator
end

That should do the trick. 👍

Reply

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?

Reply

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. https://courses.gorails.com/advanced-ruby-for-rails-devs

Reply

Wonderful, thank you =)

Reply
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.