現在のユーザーと同じ大学に通ったユーザーによって投稿されたすべての投稿を取得したいので、ウェルカムコントローラー内に次のコードを記述しました。
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
以下は、ウェルカムモデルと教育モデルのシェマコードです。
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)
しかし、これは機能していません。プロジェクトを実行すると、ページとコンソールに出力が表示されません。次の出力が表示されます。SELECT 。welcomes
* FROM welcomes
WHERE( 。IN (NULL))これは、ユーザーIDを取得しないことを意味します。どうすればこれを解決できますか...welcomes
user_id