Question

I have a have one relationship between two models. The relationship works, the problem however is that when I write a Rspec test with shoulda matchers I get returned an error.

 Failure/Error: it { should have_one(:userresetpassword)}
 NameError:
   uninitialized constant User::Userresetpassword

My code in the user.rb

has_one :userresetpassword

My rspec test

describe "associations" do
  it { should have_one(:userresetpassword)}
end
Was it helpful?

Solution

Your association is mis-named, and either rails, shoulda-matchers or both are having a hard time guessing the class name from it.

You have two choices. First, and preferable is to rename the association to be conventional:

has_one :user_reset_password

This will allow rails to correctly guess the classname UserResetPassword

Second, you could simply remove the guesswork and tell rails what the classname is. This is only preferable if you can't or very much do not want to change the association name.

has_one :userresetpassword, :class_name => "UserResetPassword"

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