单表继承与embeds_one mogoid

时间:2022-06-12 20:41:09

I have a model

我有一个模特

class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_one :comment
end

and I have comment class

我有评论课

class Comment
  include Mongoid::Document
  include Mongoid::Timestamps

  embedded_in :post

  field :title
  field :description
end

And I have another class inherited from comment

我还有另一个继承自评论的课程

class RecentComment < Comment
  # certain methods
end

Now I want to be able to create RecentComment through post if I do Post.last.build_comment(:_type => "RecentComment") the new comment will not be of _type:"RecentComment", and similarly if I do Post.last.build_recent_comment, it gives me error saying sth like undefined method build_recent_comment for Post class. If the post had references_many :comments I should have done Post.last.build_comments({}, RecentComment) without any problems. But I don't know about how to build an object with RecentComment class in this case. If anybody could help that'd be gr8!

现在我希望能够通过post创建RecentComment如果我做Post.last.build_comment(:_ type =>“RecentComment”)新评论将不是_type:“RecentComment”,同样如果我做Post.last。 build_recent_comment,它给了我一个错误,就像Post类的未定义方法build_recent_comment一样。如果帖子有references_many:评论我应该做Post.last.build_comments({},RecentComment)没有任何问题。但在这种情况下,我不知道如何使用RecentComment类构建对象。如果有人可以帮助那就是gr8!

Note: I am using gem 'mongoid', '~> 2.0.1'

注意:我正在使用gem'mongoid','〜> 2.0.1'

2 个解决方案

#1


5  

Maybe try

class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_one :recent_comment, :class_name => Comment

and just make your Comment class polymorphic

并让你的评论类多态

class Comment
  include Mongoid::Document
  include Mongoid::Timestamps

  field :type
  validates_inclusion_of :type, :in => ["recent", "other"]

#2


1  

one option is to try something like:

一种选择是尝试类似的东西:

class RecentComment < Comment
  store_in "comment"

  #set the type you want
end

but you might just use timestamps and scope to retrieve your recent, old comment, new_comment and such,

但你可能只是使用时间戳和范围来检索你最近的旧评论,new_comment等,

like within the comment class

就像在评论课中一样

scope :recent, where("created_at > (Time.now - 1.day)")

then you can do:

然后你可以这样做:

post.comments.recent

#1


5  

Maybe try

class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_one :recent_comment, :class_name => Comment

and just make your Comment class polymorphic

并让你的评论类多态

class Comment
  include Mongoid::Document
  include Mongoid::Timestamps

  field :type
  validates_inclusion_of :type, :in => ["recent", "other"]

#2


1  

one option is to try something like:

一种选择是尝试类似的东西:

class RecentComment < Comment
  store_in "comment"

  #set the type you want
end

but you might just use timestamps and scope to retrieve your recent, old comment, new_comment and such,

但你可能只是使用时间戳和范围来检索你最近的旧评论,new_comment等,

like within the comment class

就像在评论课中一样

scope :recent, where("created_at > (Time.now - 1.day)")

then you can do:

然后你可以这样做:

post.comments.recent