我有以下内容,并试图弄清楚如何选择用户信息(问题末尾的sql)。
# only global_id and list_id
class GlobalList < ActiveRecord::Base
set_table_name :globals_lists
belongs_to :list
end
# user_id
class List < ActiveRecord::Base
has_many :global_lists
belongs_to :user
end
# email
class User < ActiveRecord::Base
has_many :lists
end
假设我有一个global_id,我将如何选择用户列表的电子邮件(即假设global_id为256,
select u.* from users u, lists l, globals_lists gl where gl.global_id=256 and gl.list_id=l.id and l.user_id=u.id)
答案 0 :(得分:1)
试试这个:
User.includes({:lists => :global_lists}).where(['global_lists.global_id = ?', 256])
这将返回User
个对象,这些对象似乎是您在查询中寻找的对象。然后,您可以循环播放.email
属性中的电子邮件,或者您需要它。