创建模型
使用命令创建模型
- 描述:创建表post,默认自带两栏位 title :string, content:text , 在模型里面按照约定使用单数post而不是复数posts
- 代码(blog为创建的工程名:rails new blog)
cd blog
rails g model post title:string content:text
invoke active_record
create db/migrate/20180116065617_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.yml
- db/migrate/20180116065617_create_posts.rb文件内容
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :context
t.timestamps
end
end
end
- 以上只是生成了迁移文件,而没有正式生成数据库。 需要运行以下指令来生成数据库。
rake db:migrate
== 20180116065617 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0021s
== 20180116065617 CreatePosts: migrated (0.0029s) =============================
上述过程中报错:error mongoid [not found]
- 生成model时未报错,但是没有生成db/migrate/20180116065617_create_posts.rb文件,即未生成迁移文件
解决思路
- 查看gem包中是否有mongoid已安装:vim Gemfile
- 查看config配置中是否已有mongoid配置文件:vim config/mongoid.yml
- 未在config/application.rb中进行相关配置
config.generators do |g|
g.orm :active_record
end