embeds_many,得到孙子

时间:2012-05-15 12:18:23

标签: ruby mongodb mongoid ruby-on-rails-3.2

型号:

class User
  include Mongoid::Document

  embeds_many :user_topics
end


class UserTopic
  include Mongoid::Document

  embedded_in :user
  embeds_many :user_topic_examples
end

class UserTopicExample
  include Mongoid::Document

  embedded_in :user_topic
end

如何获取属于特定用户的所有user_topic_examples:

user.user_topic_examples

user.user_topics.user_topic_examples

感谢。

1 个答案:

答案 0 :(得分:1)

这很简单: 修订版

def user_topic_examples
  user_topics.inject([]) {|result, ut| result + ut.user_topics_examples }
end