1
私のモデルをレールでテストしています。私はRails 3.2、Ruby 1.9.3p0(2011-10-30リビジョン33570)です。[x86_64-darwin11.2.0] "User.first"と入力すると、最初のユーザーが返されます。私が "Gear.first"とタイプするとうまくいきます。Railsコンソールエラー... NameError:初期化されていない定数
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+\-.][email protected][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
:ここ
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
は私のモデル ユーザークラスです:私は「User.first.gears」のようなユーザによってギアにアクセスしようとすると、しかし、私は次のエラーを得続けます歯車クラス 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
何が欠けていますか?
おかげへのファイルの名前を変更したいが....完璧勤務しました! – DaveG