新手问题 mongoid belongs_to 的一个问题

newnewnew · April 17, 2013 · Last by saiga replied at April 17, 2013 · 2415 hits

我现在有两个 model

class User
  include Mongoid::Document
  field :name
  field :avatar
  field :sex
  ......
  has_may :topics
end
class Topic
  include Mongoid::Document
  field :title
  field :content
  ......
  belongs_to :user
end

我现在需要将某 topic 的结果通过 ajax 返回给前端

@t = Topic.where(:_id => xxx)
 render :json => @t

但是这样生成的 json 中不能包括 user 的 name,avatar 等信息,我希望将 topic 中 user 的全部信息都包含在 json 中,该怎么做呢? 另外由于实际用例比较复杂,我不能这样做 render: json => { :topic => @t, :user => @t.user } ** **我希望@t中直接包括进所属 user 的全部信息

You need to Sign in before reply, if you don't have an account, please Sign up first.