2013-02-17 36 views
1

當我打開我的Rails控制檯,運行./script/console(腳本文件夾中包含一個控制檯文件),我在控制檯輸入Users.find(:所有),我得到這個消息從鐵軌控制檯訪問數據庫

'NameError: uninitialized constant User 
    from (irb):1 
' 

我每次在控制檯中運行一個commnad時都有類似的消息(知道我沒有通過irb連接)。我搜索了一下,看到了一些類似的問題,這些問題在堆棧上被問到,我沒有得到任何滿意的答案。有人有一個想法?

Eidt 1:我不konow如果它是有用的,但這裏的建議被張貼在用戶模型類

require "digest/sha1" 
require_dependency "event" 

class User < ActiveRecord::Base 
    include UrlLinting 
    include Gitorious::Authorization 

    has_many :projects 
    has_many :memberships, :dependent => :destroy 
    has_many :groups, :through => :memberships 
    has_many :repositories, :as => :owner, :conditions => ["kind != ?", Repository::KIND_WIKI], 
    :dependent => :destroy 
    has_many :cloneable_repositories, :class_name => "Repository", 
    :conditions => ["kind != ?", Repository::KIND_TRACKING_REPO] 
    has_many :committerships, :as => :committer, :dependent => :destroy 
    has_many :commit_repositories, :through => :committerships, :source => :repository, 
    :conditions => ["repositories.kind NOT IN (?)", Repository::KINDS_INTERNAL_REPO] 
    has_many :ssh_keys, :order => "id desc", :dependent => :destroy 
    has_many :comments 
    has_many :email_aliases, :class_name => "Email", :dependent => :destroy 
    has_many :events, :order => "events.created_at asc", :dependent => :destroy 
    has_many :events_as_target, :class_name => "Event", :as => :target 
    has_many :favorites, :dependent => :destroy 
    has_many :feed_items, :foreign_key => "watcher_id" 
    has_many :content_memberships, :as => :member 

    # Virtual attribute for the unencrypted password 
    attr_accessor :password, :current_password 

    attr_protected :login, :is_admin, :password, :current_password 

    # For new users we are a little more strict than for existing ones. 
    USERNAME_FORMAT = /[a-z0-9\-_\.]+/i.freeze 
    USERNAME_FORMAT_ON_CREATE = /[a-z0-9\-]+/.freeze 
    validates_presence_of  :login, :email,    :if => :password_required? 
    validates_format_of  :login, :with => /^#{USERNAME_FORMAT_ON_CREATE}$/i, :on => :create 
    validates_format_of  :login, :with => /^#{USERNAME_FORMAT}$/i, :on => :update 
    validates_format_of  :email, :with => Email::FORMAT 
    validates_presence_of  :password,     :if => :password_required? 
    validates_presence_of  :password_confirmation,  :if => :password_required? 
    validates_length_of  :password, :within => 4..40, :if => :password_required? 
    validates_confirmation_of :password,     :if => :password_required? 
    validates_length_of  :login, :within => 3..40 
    validates_length_of  :email, :within => 3..100 
    validates_uniqueness_of :login, :email, :case_sensitive => false 
    validates_acceptance_of :terms_of_use, :on => :create, :allow_nil => false 
    validates_format_of  :avatar_file_name, :with => /\.(jpe?g|gif|png|bmp|svg|ico)$/i, :allow_blank => true 

    before_save :encrypt_password 
    before_create :make_activation_code 
    before_validation :lint_identity_url, :downcase_login 
    after_save :expire_avatar_email_caches_if_avatar_was_changed 
    after_destroy :expire_avatar_email_caches 

    state_machine :aasm_state, :initial => :pending do 
    state :terms_accepted 

    event :accept_terms do 
     transition :pending => :terms_accepted 
    end 

    end 

編輯2:當我跑我的控制檯我省略了一些錯誤消息(但控制檯負載)

/var/www/gitorious/config/environment.rb:25:RuntimeError: Your config/gitorious.yml does not have an entry for your current Rails environment. Please consult config/gitorious.sample.yml for instructions. 
/usr/lib/ruby/gems/1.8/gems/rails-2.3.14/lib/rails/backtrace_cleaner.rb:2:NameError: uninitialized constant ActiveSupport::BacktraceCleaner 
/usr/lib/ruby/gems/1.8/gems/rails-2.3.14/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController 

回答

1

假設你有一個像下面定義User型號: app/models/user.rb

class User < ActiveRecord::Base 
    #Your things 
end 

使用型號名稱(即)UserUsers

User.find(:all) or User.all # This will display all user records 
+0

當然,我得到了同樣的信息:NameError:未初始化的常數用戶 \t從(IRB):3 – Newben 2013-02-17 16:25:57

+0

那麼你不必在'應用程序/ models'定義'User'模型。檢查你的'應用程序/模型'n告訴你有什麼.. – codeit 2013-02-17 16:28:25

+0

當然,我創建用戶的帳戶,我可以登錄與他們沒有p – Newben 2013-02-17 16:30:46

1

這裏是沒有的伎倆:不是./script/console跑,我跑

export RAILS_ENV=production && ./script/console 
1

從錯誤中,我可以看到你從irb調用表。 如果你想從鐵軌控制檯訪問表,然後在命令行,而不是irb應鍵入:

rails console 

其次,如果你想獲得所有從用戶表中的記錄,你應該使用:

User.all