使用postgres时在Rails控制台中创建对象时出错

时间:2015-05-21 04:08:13

标签: ruby-on-rails postgresql

尝试使用Postgres在我的生产机器上的rails控制台中创建一个对象时出现错误,我没有在我的开发机器上(当前使用的是sqlite3)。

我有两张表settingsarticlessettingsarticles的父级,因此“设置”:has_many“文章”和“文章”属于一个“设置”。它们看起来像我的架构文件。

create_table "articles", force: :cascade do |t|
    t.string   "article_name"
    t.string   "xml_file"
    t.string   "xslt_file"
    t.integer  "setting_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

  add_index "articles", ["setting_id"], name: "index_articles_on_setting_id"

create_table "settings", force: :cascade do |t|
    t.string   "commentaryid"
    t.string   "logo"
    t.text     "title"
    t.text     "bannermessage"
    t.boolean  "blog"
    t.string   "default_ms_image"
    t.string   "dark_color"
    t.string   "light_color"
    t.string   "commentarydirname"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.boolean  "images"
  end

这些类的模型如下:

class Article < ActiveRecord::Base
  belongs_to :setting
end

class Setting < ActiveRecord::Base
  attr_reader :confighash
  has_many :articles, dependent: :destroy
  after_initialize :set_confighash

 private
  def set_confighash()
    @confighash = { }
    end
end

我认为我正确设置了这些,并且在我的开发机器上使用sqlite3一切正常。

但是,在Heroku服务器的rails控制台上(使用postgres)我每次尝试创建一个Article对象时都会收到错误,如下所示:

Article.create(article_name: 'biography') 

错误非常神秘。它写着:

NoMethodError: undefined method `[]' for nil:NilClass
    from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:375:in `clear_transaction_record_state'
    from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:306:in `ensure in rollback_active_record_state!'
    from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:306:in `rollback_active_record_state!'
    from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:285:in `save'
    from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:34:in `create'
    from (irb):1
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from /app/bin/rails:8:in `require'
    from /app/bin/rails:8:in `<main>'

同样,在使用sqlite3时,使用相同的模式创建Article对象时没有问题。

我认为我定义关系的方式有问题。

请帮忙。

0 个答案:

没有答案