我正在使用rails测试我的模型中的关联。我在Rails 3.2和ruby 1.9.3p0(2011-10-30修订版33570)[x86_64-darwin11.2.0] 当我输入“User.first”时,它工作正常,返回第一个用户。当我键入“Gear.first”时,它也可以正常工作。但是,当我尝试通过用户访问“User.first.gears”之类的设备时,我不断收到以下错误:
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
这是我的模特 用户类:
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 UserGears < ActiveRecord::Base
belongs_to :gear
belongs_to :user
end
我错过了什么?
答案 0 :(得分:6)
您想将UserGears
课程重命名为UserGear
并将文件重命名为user_gear.rb