rails嵌套关联上的多级include



我有3个表、注释、用户和图像。

我正在尝试制定一个查询,该查询将在帖子上生成评论数组,其中包括评论员的信息和头像。

化身存储在图像表中,而用户表包含用户信息加上存储用户化身的图像对象的参考id。

每个注释都有一个引用用户表中对象的作者id。

这就是我目前拥有的

@comments = Comment.all(:include => [:user => :images], 
            :conditions => {
              :source => p[:source], 
              :source_id => p[:id], 
              :users => {:id => p[:user_id]}, /* if this result is *user */
              :images => {????} /*essentially i need images.id = *user.profile_id */
              })

无法使图像部分工作,有人能告诉我如何工作吗?

这就是我在结束时所做的工作

class User < ActiveRecord::Base
    belongs_to :image, :foreign_key => "profile_id"
class Image < ActiveRecord::Base
    has_one :user
class Comment < ActiveRecord::Base
    belongs_to :user
@comments = Comment.all(:include => [{:user => :image}, :like, :flag], 
            :conditions => {
              ...
              })

这是有效的,但如果有人能告诉我这是否是正确的方法,那将是一个很好的

最新更新