简体   繁体   中英

Strange “undefined method” error in Ruby on Rails app after switching to Postgres

Here's prompt.rb :

class Prompt < ActiveRecord::Base
  attr_accessible :title, :teacherUrl, :studentUrl, :image, :text;

  validates :title, :length => { :minimum => 5 };

  has_attached_file :image ;

  def initialize(params = nil)
    super(params)
    after_initialize
  end

  def after_initialize()
    self.teacherUrl = 'T' + (0...4).map{65.+(rand(26)).chr}.join ;
    self.studentUrl = 'S' + (0...4).map{65.+(rand(26)).chr}.join ;
  end
end

When I create a new Prompt from prompt_controller.rb (from the default 'new' method) I get:

undefined method `teacherUrl=' for #<Prompt:0x007fe3ac68adc0>

with the application trace

app/models/prompt.rb:14:in `after_initialize'
app/models/prompt.rb:10:in `initialize'
app/controllers/prompts_controller.rb:43:in `new'
app/controllers/prompts_controller.rb:43:in `new'

This was working perfectly with sqlite. I've just switched my databases to Postgres and now this error is rising... it seems like it couldn't be related, but that's all that's changed (I can go back a revision and it works perfectly again).

What's the problem? Isn't teacherUrl defined with attr_accessible?

You need to do rake db:migrate . And possibly rake db:create (first).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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