Active record error messages are Double?

Hi Friends

I have two below classes

class Employee< ActiveRecord::Base
validates_presence_of :name,

end
other
class AuditObserver < ActiveRecord::Observer
observe Employee


end

But when we are creating the Employee record form create view

All predefined error messages are twice

Means validation error

Name can’t be blank

Name can’t be blank

Could please give any advice on it ? Why the error messages are
Double?

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.

Aaron

On Jan 17, 8:51 am, Aaron [email protected] wrote:

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.

But #require shouldn’t load employee twice, unless it’s given two
different paths, right?

///ark

I had the exact same problem as described in the original post. I
tracked it down to some explicit require statements I had at the top
of one of my models. When I removed the requires the double error
messages went away.

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.

Aaron

I didn’t call require in any models

But I have called require in employee controller

Is that any problem?

But the error messages twice in only testing , production
Error messages are fine in development mode

I am having the same issue. I do not have any extra requires. Has
anyone found a solution to this issue? I am using the basic
scaffolding generated by rails.

On Jan 17, 7:18 am, Karni K. [email protected]

ash.christopher wrote:

I am having the same issue. I do not have any extra requires. Has
anyone found a solution to this issue? I am using the basic
scaffolding generated by rails.

On Jan 17, 7:18�am, Karni K. [email protected]

try production mode?

On Jan 22, 12:58 pm, Karni K. [email protected]
wrote:

I didn’t call require in any models

But I have called require in employee controller

Is that any problem?

But the error messages twice in only testing , production
Error messages are fine in development mode
That’s because of the magic class reloading stuff. scrap the require
in employee controller and you’ll probably be ok.

Fred

Karni K. wrote:

I didn’t call require in any models

But I have called require in employee controller

Is that any problem?

But the error messages twice in only testing , production
Error messages are fine in development mode

maybe calling ‘require_dependency’ instead of ‘require’ would help?

Thanks a lott Roger P. !
Including via require_dependency rather than require does solve the
mystery…:slight_smile: