连接到外部MySQL数据库时出现“NameError:uninitialized constant”错误

时间:2015-09-13 07:33:33

标签: mysql ruby-on-rails

运行简单的控制台命令(例如Release.first(也尝试过release.first)时出现此错误。在尝试其他表时,我也遇到了类似的错误。

我的release.rb

module default
  class Release < ActiveRecord::Base
    self.table_name = 'releases'
    self.inheritance_column = 'ruby_type'
    self.primary_key = 'id'

    if ActiveRecord::VERSION::STRING < '4.0.0' || defined?(ProtectedAttributes)
      attr_accessible :artist, :all_artists, :format, :title, :label, :label_no_country, :link, :genre, :date, :image, :favourite, :time, :category, :format_category, :soundclip_1, :soundclip_2, :spotify, :digital_link, :display, :zero_inch_id, :zero_inch_link, :zero_release_url
    end

    has_many :alerts_sents, :foreign_key => 'release_id', :class_name => 'AlertsSent'
    has_many :audio_links, :foreign_key => 'release_id', :class_name => 'AudioLink'
    has_many :charts_extendeds, :foreign_key => 'release_id', :class_name => 'ChartsExtended'
    has_many :juno_sales, :foreign_key => 'release_id', :class_name => 'JunoSale'
    has_many :not_recommendeds, :foreign_key => 'release_id', :class_name => 'NotRecommended'
    has_many :num_release_recs, :foreign_key => 'release_id', :class_name => 'NumReleaseRec'
    has_many :recommendations, :foreign_key => 'release_id', :class_name => 'Recommendation'
    has_many :release_artists, :foreign_key => 'release_id', :class_name => 'ReleaseArtist'
    has_many :spotifies, :foreign_key => 'release_id', :class_name => 'Spotify'
    has_many :twitter_posteds, :foreign_key => 'release_id', :class_name => 'TwitterPosted'
    has_many :user_adds, :foreign_key => 'release_id', :class_name => 'UserAdd'
  end
end

错误是

NameError: uninitialized constant Release
    from (irb):1
    from /Users/siquick/.rvm/gems/ruby-2.1.3/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
    from /Users/siquick/.rvm/gems/ruby-2.1.3/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/siquick/.rvm/gems/ruby-2.1.3/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/siquick/.rvm/gems/ruby-2.1.3/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/siquick/.rvm/gems/ruby-2.1.3/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/siquick/Rails/soundshelter/bin/rails:8:in `<top (required)>'
    from /Users/siquick/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/siquick/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

数据库位于远程服务器上,我使用rare_map

生成模型

schema.rb

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150910131323) do

  create_table "releases", force: :cascade do |t|
    t.string  "artist",           limit: 255,   null: false
    t.string  "all_artists",      limit: 200,   null: false
    t.string  "format",           limit: 80,    null: false
    t.string  "title",            limit: 255,   null: false
    t.string  "label",            limit: 255,   null: false
    t.string  "label_no_country", limit: 255,   null: false
    t.text    "link",             limit: 65535, null: false
    t.string  "genre",            limit: 50,    null: false
    t.string  "date",             limit: 80,    null: false
    t.text    "image",            limit: 65535, null: false
    t.string  "favourite",        limit: 1,     null: false
    t.string  "time",             limit: 20,    null: false
    t.string  "category",         limit: 30,    null: false
    t.string  "format_category",  limit: 20,    null: false
    t.string  "soundclip_1",      limit: 255,   null: false
    t.string  "soundclip_2",      limit: 255,   null: false
    t.string  "spotify",          limit: 100,   null: false
    t.string  "digital_link",     limit: 255,   null: false
    t.string  "display",          limit: 1,     null: false
    t.integer "zero_inch_id",     limit: 4,     null: false
    t.text    "zero_inch_link",   limit: 65535, null: false
    t.string  "zero_release_url", limit: 150,   null: false
  end

  add_index "releases", ["all_artists"], name: "all_artists", using: :btree
  add_index "releases", ["all_artists"], name: "all_artists_2", using: :btree
  add_index "releases", ["all_artists"], name: "all_artists_3", using: :btree
  add_index "releases", ["all_artists"], name: "all_artists_4", using: :btree
  add_index "releases", ["all_artists"], name: "all_artists_5", using: :btree
  add_index "releases", ["artist"], name: "artist", using: :btree
  add_index "releases", ["artist"], name: "artist_2", using: :btree
  add_index "releases", ["artist"], name: "artist_3", using: :btree
  add_index "releases", ["artist"], name: "artist_4", using: :btree
  add_index "releases", ["category"], name: "category", using: :btree
  add_index "releases", ["date"], name: "date", using: :btree
  add_index "releases", ["format"], name: "format", using: :btree
  add_index "releases", ["format_category"], name: "format_category", using: :btree
  add_index "releases", ["format_category"], name: "format_category_2", using: :btree
  add_index "releases", ["genre"], name: "genre", using: :btree
  add_index "releases", ["genre"], name: "genre_2", using: :btree
  add_index "releases", ["genre"], name: "genre_3", using: :btree
  add_index "releases", ["genre"], name: "genre_4", using: :btree
  add_index "releases", ["id"], name: "id", using: :btree
  add_index "releases", ["id"], name: "id_2", using: :btree
  add_index "releases", ["label"], name: "label", using: :btree
  add_index "releases", ["label"], name: "label_2", using: :btree
  add_index "releases", ["label_no_country"], name: "label_no_country", using: :btree
  add_index "releases", ["label_no_country"], name: "label_no_country_2", using: :btree
  add_index "releases", ["label_no_country"], name: "label_no_country_3", using: :btree
  add_index "releases", ["label_no_country"], name: "label_no_country_4", using: :btree
  add_index "releases", ["label_no_country"], name: "label_no_country_5", using: :btree
  add_index "releases", ["spotify", "zero_release_url"], name: "spotify_2", using: :btree
  add_index "releases", ["spotify"], name: "spotify", using: :btree
  add_index "releases", ["title"], name: "title", using: :btree
  add_index "releases", ["zero_inch_id"], name: "zero_inch_id", using: :btree

  add_foreign_key "releases", "charts_extended", column: "id", primary_key: "release_id", name: "releases_ibfk_1"
end

1 个答案:

答案 0 :(得分:1)

您的Release类是在default模块中定义的(由于Ruby类/模块应该从大写开始,btw也会拼写错误),因此您将无法仅使用{{{{}}来引用它。 1}}在模块范围之外。

更改定义并取出模块。

Report

您可能还想删除所有各种不必要的设置,例如 class Release < ActiveRecord::Base self.table_name = 'releases' self.inheritance_column = 'ruby_type' self.primary_key = 'id' if ActiveRecord::VERSION::STRING < '4.0.0' || defined?(ProtectedAttributes) attr_accessible :artist, :all_artists, :format, :title, :label, :label_no_country, :link, :genre, :date, :image, :favourite, :time, :category, :format_category, :soundclip_1, :soundclip_2, :spotify, :digital_link, :display, :zero_inch_id, :zero_inch_link, :zero_release_url end has_many :alerts_sents, :foreign_key => 'release_id', :class_name => 'AlertsSent' has_many :audio_links, :foreign_key => 'release_id', :class_name => 'AudioLink' has_many :charts_extendeds, :foreign_key => 'release_id', :class_name => 'ChartsExtended' has_many :juno_sales, :foreign_key => 'release_id', :class_name => 'JunoSale' has_many :not_recommendeds, :foreign_key => 'release_id', :class_name => 'NotRecommended' has_many :num_release_recs, :foreign_key => 'release_id', :class_name => 'NumReleaseRec' has_many :recommendations, :foreign_key => 'release_id', :class_name => 'Recommendation' has_many :release_artists, :foreign_key => 'release_id', :class_name => 'ReleaseArtist' has_many :spotifies, :foreign_key => 'release_id', :class_name => 'Spotify' has_many :twitter_posteds, :foreign_key => 'release_id', :class_name => 'TwitterPosted' has_many :user_adds, :foreign_key => 'release_id', :class_name => 'UserAdd' end 并删除table_name,除非您使用旧版本的Rails。

看起来你使用的生成器添加了一堆不必要的代码。