我在终端输入:
rails g migration rename_blog
然后在生成的 migration 文件里面写:
class RenameBlog < ActiveRecord::Migration
def up
rename_table :blogs, :articles
end
def down
rename_table :articles, :blogs
end
end
rake db:migrate
之后,在 schema 里面看到:
ActiveRecord::Schema.define(:version => 20120829053310) do
create_table "articles", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "content"
t.string "author"
end
end
已经更改过来了,但是我尝试:
rails c
1.9.3p194 :004 > Article.column_names
#以下是返回的错误
NameError: uninitialized constant Article
from (irb):4
from /Users/Yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /Users/Yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /Users/Yuwen/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我搜索了一下,改原先的 model 类名Blog
改为Article
,并且把该文件blog.rb
改为article.rb
之后,还是这样。
另外那个up
和down
方法的意思不是很明确,怎么又是颠倒过来,哪个是上,哪个是下?