在 Rails Mongoid 中使用 belongs_to 与 embedded_in
Use of belongs_to vs embedded_in in Rails Mongoid
Rails 6
MongoDB
Mongoid
我正在关注 MongoDB 网站,简短且未记录的教程示例。
有几个模型:
Post
Comment
我创建了Post,如下:
rails g scaffold Post title:string body:text
然后创建Comment,如下:
rails g scaffold Comment name:string message:string post:belongs_to
这是自动生成的代码:
models/post.rb:
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :body, type: String
has_many :comments, dependent: :destroy
end
models/comment.rb:
class Comment
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :message, type: String
embedded_in :post
end
在 comment.rb 中,我预计:
belongs_to :post
embedded_in 是正确的用法吗,还是我应该将其更改为:belongs_to?
The tutorial 指示将 embedded_in
更改为 belongs_to
。
Rails 6
MongoDB
Mongoid
我正在关注 MongoDB 网站,简短且未记录的教程示例。
有几个模型:
Post
Comment
我创建了Post,如下:
rails g scaffold Post title:string body:text
然后创建Comment,如下:
rails g scaffold Comment name:string message:string post:belongs_to
这是自动生成的代码:
models/post.rb:
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :body, type: String
has_many :comments, dependent: :destroy
end
models/comment.rb:
class Comment
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :message, type: String
embedded_in :post
end
在 comment.rb 中,我预计:
belongs_to :post
embedded_in 是正确的用法吗,还是我应该将其更改为:belongs_to?
The tutorial 指示将 embedded_in
更改为 belongs_to
。