Ask A Question

Notifications

You’re not receiving notifications from this thread.

uninitialized constant User::shift

geekdie asked in Rails

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!

Reply

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.

Reply

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

Reply

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
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.