【发布时间】:2012-04-09 05:06:41
【问题描述】:
我想获取与当前用户上同一所大学的用户发布的所有帖子......所以在我的欢迎控制器中,我编写了以下代码..
class WelcomesController < ApplicationController
def index
@col = Education.select(:college_id).where(:user_id => @current_user)
@user = Education.select(:user_id).where(:college_id => @col)
@welcome = Welcome.where(:user_id => @user)
end
end
以下是我的欢迎和教育模型的 shema 代码:
create_table "welcomes", :force => true do |t|
t.text "message"
t.integer "user_id"
end
create_table "educations", :force => true do |t|
t.integer "college_id"
t.integer "user_id"
end
@col = Education.select(:college_id).where(:user_id => @current_user)....此行返回与当前登录用户关联的大学 ID。这在我的控制台上运行良好,返回以下输出..
[#<Education college_id: 1>, #<Education college_id: 2>]
但我不知道如何在下一行使用这个输出,所以我写了这个语句,它应该返回所有大学 id 是前一个语句输出的用户
@user = Education.select(:user_id).where(:college_id => @col)
我的最后一行应该返回那些 id 在 @user 数组中的用户发布的所有帖子:
@welcome = Welcome.where(:user_id => @user)
但这不起作用。当我运行我的项目时,我在页面和控制台上看不到任何输出,我得到以下输出:
选择welcomes.* FROM welcomes WHERE (welcomes.user_id IN (NULL))
这意味着它没有得到任何用户ID..
我该如何解决这个问题...
【问题讨论】:
-
使用粗体文本会使事情变得更难阅读,并且人们不再知道什么是重要的,因此避免在长句中使用粗体文本,而是用它来强调真正需要强调的内容。
-
好的,Zabba 会照顾好你所说的......
标签: ruby-on-rails arrays ruby-on-rails-3 model-view-controller controller-action