Rails : uninitialized constant error on Active Record destroy
Asked Answered
T

3

14

I am having an issue when trying to destroy an active record instance.

It involves the following AR

class Client < ActiveRecord::Base
    has_many :phone_numbers, :dependent => :destroy
    has_many :email_addresses, :dependent => :destroy
    has_many :user_clients , :dependent => :destroy
    has_many :users, :through => :user_clients 
end

class UserClient  < ActiveRecord::Base
belongs_to :user
belongs_to :client , :dependent => :destroy
has_many :instructions, :dependent => :destroy
end

When performing a destroy on a Client instance I am given the following error

@dead_man = Client.find(params[:id])
@dead_man.destroy => uninitialized constant UserClient::Instruction

I am really not sure where this error is coming from. Any help is greatly appreciated!

Tauten answered 7/4, 2009 at 22:9 Comment(0)
C
20

It's not finding your Instruction model. Make sure it's in the models directory, appropriately named, extends ActiveRecord::Base, etc.

Also, you should remove the :dependent => :destroy from the belongs_to :client line in the UserClient model, unless you really want deletion of a user_client to result in deletion of the client. It sounds like it should be the other way around, and that's already set up in the Client model.

Carner answered 7/4, 2009 at 22:17 Comment(0)
R
4

Also check that the file name corresponds with the class name. In my case I had

Class NameSpace::MyStats

in

namespace/old_stats.rb

and Rails kept on throwing the "uninitialized constant error" until I changed it to

namespace/my_stats.rb
Riane answered 6/9, 2012 at 10:35 Comment(0)
B
0

In my case, it was not finding the correct class name because of pluralize. So, I specified the class name explicitly in my association.

For you, it would look like:

has_many :instructions, class_name: "Instruction", :dependent => :destroy
Boresome answered 3/7 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.