Question

I have a User model in my Rails application and I have a UserQueue model as well.

User has_many UserQueues and UserQueue belongs_to User.

Here is the problem. When I try to test UserQueue and try to create one with

let(:user) { FactoryGirl.create(:user) }
before { @queue = user.user_queues.create(queue_privacy_id: 1) }

I get the following error.

NameError:
   uninitialized constant User::user_queue

What I understand from this is RSpec expects the UserQueue to be in the namespace of User(ie User::UserQueue). However that is not the case in my app. And I can't name the model Queue since it is reserved.

Is there a way to tell RSpec that the model has no namespace?

Here are my models.

class User < ActiveRecord::Base
  attr_protected

  has_many :user_queues, :class_name => "user_queue", :foreign_key => "user_id"

  def name
    "#{self.first_name} #{self.last_name}"
  end
end


class UserQueue < ActiveRecord::Base
  attr_accessible :queue_privacy_id, :user_id

  belongs_to :user, :class_name => "User", :foreign_key => "user_id"
end
Was it helpful?

Solution

I do not have the rep to comment, but was going to ask you to post your factories as well.

Some other ideas: Take a look at how you can handle assocations with FactoryGirl. I'm assuming you are not on Rails 4 because of:

attr_accessible :queue_privacy_id, :user_id

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top