Ruby: Getting Active Record validation errors twice
I managed to create an interesting problem for myself while playing around with some code whereby I was ending up with validation errors appearing twice every time I called 'valid?' on a specific model.
I figured I was probably doing something stupid and in fact a few replies by Aaron Baldwin on a mailing list thread on 'rubyonrails-talk' helped explain exactly what I’d done:
Are you calling require 'employee' anywhere? If so you are likely causing the model to load twice which causes duplicate errors because the validates_presence_of method gets called twice.
I’d put the following code into a controller elsewhere somewhat unnecessarily since it didn’t seem to be picking up the location of my model at the time:
code_submissions_controller.rb
require 'models/code_submission'
class CodeSubmissionsController < ApplicationController
def new
CodeSubmission.new
end
end
require doesn’t load a file if it’s already been included but Aaron points out why it does on this occasion:
You are right that "require" will only load the file once. But if you load the class another way calling "require" will load it again.
As I understand it that controller code would also implicitly require 'code_submission' by the convention of inserting an underscore between the 'CodeSubmission' constant’s names.
We therefore effectively have the following two requires:
require 'code_submission'
require 'models/code_submission'
Which explain how the file gets loaded twice and therefore why the validation method fires twice and therefore creates two errors!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.