作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个模型
class Post
include Mongoid::Document
include Mongoid::Timestamps
embeds_one :comment
end
class Comment
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :post
field :title
field :description
end
class RecentComment < Comment
# certain methods
end
RecentComment
通过
post
如果我这样做
Post.last.build_comment(:_type => "RecentComment")
新评论不会是
_type:"RecentComment"
,同样如果我这样做
Post.last.build_recent_comment
,它给我错误说某事像
undefined method build_recent_comment for Post class
.如果
post
有
references_many :comments
我应该做的
Post.last.build_comments({}, RecentComment)
没有任何问题。但我不知道如何使用
RecentComment
构建对象在这种情况下类。如果有人能帮忙,那就是gr8!
gem 'mongoid', '~> 2.0.1'
最佳答案
也许试试
class Post
include Mongoid::Document
include Mongoid::Timestamps
embeds_one :recent_comment, :class_name => Comment
class Comment
include Mongoid::Document
include Mongoid::Timestamps
field :type
validates_inclusion_of :type, :in => ["recent", "other"]
关于ruby-on-rails - 带有 embeds_one mogoid 的单表继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5963879/
我有一个模型 class Post include Mongoid::Document include Mongoid::Timestamps embeds_one :comment en
我理解使用 embeds_many 的概念但是embeds_one有什么用途尤其是当您在嵌入式文档中只有一个字段时,如 mongoid.org 上的 1-N 嵌入式文档示例所示?在父文档中创建一个字段
我有两个模型,博客和主题。博客 embeds_many :themes 和 Theme embedded_in :blog。我也有 Blog embeds_one :theme (用于激活的主题)。这
我是一名优秀的程序员,十分优秀!