Reputation: 1203
I'm testing my assocations in my models in rails. I'm on Rails 3.2, and ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0] When I type "User.first" it works fine, returning the first user. When I type "Gear.first" it also works fine. However when I try to access gear through the user like "User.first.gears" I keep getting the following error:
NameError: uninitialized constant User::UserGear
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/inheritance.rb:119:in `compute_type'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/reflection.rb:172:in `klass'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/reflection.rb:385:in `block in source_reflection'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/reflection.rb:385:in `collect'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/reflection.rb:385:in `source_reflection'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/reflection.rb:508:in `check_validity!'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/associations/association.rb:26:in `initialize'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/associations/collection_association.rb:24:in `initialize'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/associations/has_many_through_association.rb:10:in `initialize'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/associations.rb:157:in `new'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/associations.rb:157:in `association'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.0/lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
from (irb):2
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands/console.rb:47:in `start'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands/console.rb:8:in `start'
from /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.0/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p0 :003 > exit
Here are my models User Class:
class User < ActiveRecord::Base
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation
has_secure_password
has_many :gears, through: :user_gears
has_many :user_gears
before_save :create_remember_token
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :first_name, presence: true,
length: {:maximum => 50 }
validates :last_name, presence: true,
length: {:maximum => 50 }
validates :email, presence: true,
format: {:with => email_regex},
uniqueness: {:case_sensitive => false}
validates :password, presence: true,
confirmation: true,
length: {within: 6..40}
def name
first_name + " " + last_name
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
Gears Class
class Gear < ActiveRecord::Base
attr_accessible :title, :size, :price, :image_url, :sub_category_id
has_many :users, through: :user_gears
has_many :user_gears
belongs_to :sub_category
validates :title, presence: true
validates :size, presence: true
validates :price, presence: true
validates :sub_category_id, presence: true
end
User_Gears Class
class UserGears < ActiveRecord::Base
belongs_to :gear
belongs_to :user
end
What am I missing?
Upvotes: 1
Views: 7640
Reputation: 600
You want to rename your UserGears
class to UserGear
and rename the file to user_gear.rb
Upvotes: 6