轨道上嵌套的关联,多层次包括: [英] rails multiple level include on nested association
问题描述
我有3个表,注释,用户,和图像。
I have 3 tables, comments, users, and images.
我试图制定一个查询,将在一个职位,其中包括解说员的信息和头像评论的产品阵列。
I'm trying to formulate a query that will product arrays of comments on a post that includes the commentator's information and a avatar.
化身存储在图像表,而用户表包含用户信息加一个参考id来存储该用户的化身的图像对象
The avatar is stored in the images table, while the users table contains the user info plus a reference id to the image object that stores the users avatar.
每个评论有一个作者的ID引用一个对象中的用户表。
Each comment has a author id that is referencing an object in the user table.
这是我迄今为止
@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 */
})
无法获取图像部分的工作,有人可以告诉我怎么样?
Can't get the image part to work, can someone show me how?
推荐答案
这是我在最后得到了工作
This is what I got working at the end
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 => {
...
})
这工作,但如果有人能告诉我,如果它的正确的方式做到这一点,将是巨大的。
This works but if anyone can tell me if its the proper way to do this it would be great
这篇关于轨道上嵌套的关联,多层次包括:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!