简体   繁体   English

连接到外部MySQL数据库时出现“ NameError:未初始化常量”错误

[英]"NameError: uninitialized constant' error when connecting to external MySQL database

Getting this error when running simple console commands such as Release.first (also tried release.first ). 运行简单的控制台命令(例如Release.first (也尝试过release.first ))时出现此错误。 I am also getting a similar error when trying work on other tables. 尝试在其他表上工作时,我也遇到类似的错误。

My release.rb is 我的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

The error is 错误是

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>'

The database is on a remote server and I generated the models using rare_map 该数据库位于远程服务器上,我使用了稀有地图生成了模型

schema.rb 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

Your Release class is defined within a default module (which btw is also misspelled as Ruby class/modules should start uppercase), therefore you won't be able to reference it with just Report outside the module scope. 您的Release类是在default模块中定义的(btw也拼写错误,因为Ruby类/模块应该以大写字母开头),因此您将无法仅使用Report在模块范围之外进行引用。

Change the definition and take out the module. 更改定义并取出模块。

  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

You may also want to remove all the various unnecessary setting such as the table_name and remove the attr_accessible unless you are using an old version of Rails. 除非您使用的是旧版本的Rails,否则您可能还希望删除所有各种不必要的设置,例如table_name并删除attr_accessible

It looks like the generator you used added a bunch of unnecessary code. 看来您使用的生成器添加了许多不必要的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM