Skip to main content

uninitialized constant User::shift

Rails • Asked by geekdie

I got this error: uninitialized constant User::shift
here's the stack trace

def redirect_based_on_shift_state
if current_user
created_shifts = current_user.shifts.in_state(:created).first
redirect_to new_shift_call_list_path(created_shifts)
end
end

Class < User
self.primary_key = 'guid'
has_many :shifts, foreign_key: 'user_guid', primary_key: 'guid', class_name: 'shift'

Class < Shift
belongs_to :user, foreign_key: 'user_guid', primary_key: 'guid', class_name: 'user'

Thanks for the help!


Hey geekdie,

At a glance, try removing class_name from both of your associations. There's no need to do that (as far as I'm aware) unless the class name is different than the name you want to use when calling the association.


the class name is not different, I added that hoping that will fix the problem but it did not.


Look at your foreign/primary key assignments on your Shift model, that should be:

belongs_to :user, foreign_key: 'guid', primary_key: 'user_guid'

After you adjust the foreign/primary key names if it still doesn't work, double check your model file names and their class declarations to ensure there's no typos.

Another thing I just noticed, are your models inheriting from ApplicationRecord? Your code doesn't show that to be the case. They should be:

class Shift < ApplicationRecord
  ...
end

and

class User < ApplicationRecord
  ...
end

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.