7

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!

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

Sadiksha Gautam
  • 5,032
  • 6
  • 40
  • 71
  • You probably just need to put each comment type explicitly into the Post class- embeds_one :old_comment; embeds_one :new_comment ... – monocle May 21 '11 at 19:04
  • I guess that's one of the problems with creating subclasses of Comment. Is there any benefit of having the subclasses? You can have a lot of classes that are all the same except the names. If it's possible you might want to refactor now before it becomes harder. Otherwise I'm not sure how to create the associations that you want without explicitly making each one an embedded doc. – monocle May 23 '11 at 04:55
  • 1
    but all the derived classes have same fields and properties as comment so will it be appropriate to make all of them different embedded doc? – Sadiksha Gautam May 23 '11 at 05:21
  • 1
    it is already late.. i have all my controllers and methods using that association. maybe i asked the question too late. Anyways thanks for trying to help! – Sadiksha Gautam May 23 '11 at 05:23
  • What is the difference between a RecentComment and regular Comment? – Jack Chu May 25 '11 at 06:36

2 Answers2

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"]
monocle
  • 5,866
  • 2
  • 26
  • 22
  • 2
    actually there are other derived classes in comments like recentcomment, oldcomment and newcomment. If there was only one derived class i am sure your answer would have been ideal. – Sadiksha Gautam May 21 '11 at 10:40
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,

like within the comment class

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

then you can do:

post.comments.recent
Andres
  • 11
  • 1