以前使用 AR 的时候,加了属性总是会 rake db:migrate 下使之生效,改成 mongoid 连接后面的 mongodb 存储后,应该是没有这个 rake 的步骤了,直接在 model 上加属性就可以了。但是使用的时候总是会提示找不到属性,如“undefined method `likes' for #”,这个要怎么破呢?还是说也有类似 rake db:migrate 这样的动作来对数据进行更新?
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Taggable
paginates_per 20
field :title
field :category
field :subcategory
field :likes, type: Integer, default: 0
field :dislikes, type: Integer, default: 0
field :from
field :reference
field :summary
field :content
index({ title: 1 })
index({ category: 1})
rake db:mongoid:purge
把整个 collections drop 掉重新创建都不成了,mongodb 里边新创建的记录还是没后面加的 likes 和 dislikes 这两属性。
#10 楼 @Ddl1st 看着是 development 的
zhangner@elementary:~/workspace/concern$ rails c
Loading development environment (Rails 4.0.0)
1.9.3p392 :001 >
zhangner@elementary:~/workspace/concern$ rails s
=> Booting Thin
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
#15 楼 @Ddl1st 木有新加的 filed 的,跟之前通过 robomongo 连过去 mongodb 看的一样。
zhangner@elementary:~/workspace/concern$ rails c
Loading development environment (Rails 4.0.0)
1.9.3p392 :001 > News.fields.keys
=> ["_id", "created_at", "updated_at", "tags_array", "title", "category", "subcategory", "from", "reference", "summary", "content"]
1.9.3p392 :002 >