Fingerfactor
Fingerfactor

Reputation: 161

Why does my controller have access to one model, but not another?

I have a controller named User. In two of its actions, I would like to access a model named Preference. I receive an "uninitialized constant UserController::Preferences" error when trying to tap either action.

I am able to access other models such as Prospect without error. Both Prospect and Preference are related have a belongs_to relationship with User.

I've tried accessing the model as ::Preferences, but I receive this very similar error: "uninitialized constant Preferences". I've also tried manually requiring the model, to no avail.

Has anyone else run into this problem and come up with a solution?

Upvotes: 0

Views: 266

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

It looks like you are calling it "Preferences". But that is almost certainly not the name of your model.

class UsersController < ApplicationController

  def show #or whatever
    @user = User.find(67)
    @preferences = user.preferences
    # OR
    @preferences = Preference.where(:user_id => @user.id).first
  end
end

Note that when we call Preference, it is singular.

Upvotes: 2

Related Questions