0

現在のユーザーと同じ大学に通ったユーザーによって投稿されたすべての投稿を取得したいので、ウェルカムコントローラー内に次のコードを記述しました。


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 welcomesWHERE( 。IN (NULL))これは、ユーザーIDを取得しないことを意味します。どうすればこれを解決できますか...welcomesuser_id

4

3 に答える 3

0

あなたはこれを試すことができます:

@col = Education.select(:college_id).where(:user_id => @current_user.id).all
@users = Education.select(:user_id).where(:college_id => @col.collect(&:college_id)).all
@welcome = Welcome.where(:user_id => @users.collect(&:user_id)).all
于 2012-04-09T06:37:04.157 に答える
0

これを達成するために私が見る最善の方法は、ユーザーモデルと教育モデルの間にhas_many_and_belongs_to_many関係を設定することです。(各教育には多くのユーザーがいて、各ユーザーには複数の教育がある場合があります。)このタイプの関係をサポートするには、データベースに結合テーブルを作成する必要があります。詳細については、Railsガイドを参照してください。

私はあなたのモデルをこのようにセットアップします:

class User < ActiveRecord::Base
  has_one :welcome
  has_and_belongs_to_many :educations
end

class Education < ActiveRecord::Base
  has_and_belongs_to_many :users
end

class Welcome < ActiveRecord::Base
  belongs_to :user
end

has_many_and_belongs_to_many結合テーブル移行の結合テーブル(このコードを再確認してください。これが正確に正しいかどうかはわかりません):

def self.up
  create_table 'education_user', :id => false do |t|
    t.column :education_id, :integer
    t.column :user_id, :integer
  end
end

これで、コントローラーコードがはるかに単純になり、次のようになります。

@welcomes = @current_user.eductions.users.welcome.all

あなたの見解では:

<% @welcomes.each do |welcome| %>
  <p><%= welcome.message %></p>
<% end %>

Ruby on Railsのより強力な機能の1つは、モデルの関係です。これらは前もってもう少し作業が必要ですが、時間をかけて正しく設定すると、上記の簡略化された@welcomesクエリで証明されているように、作業がはるかに楽になります。

于 2012-04-09T06:56:18.460 に答える
0

ユーザーとコラージュの関係を築くことをお勧めします

class User < ActiveRecord::Base
  has_many :educations
  has_many :colleges, :through => :educations
  has_many :posts

  scope :by_college_id, lambda {|cid| where("exists (select educations.id from educations where educations.user_id = users.id AND educations.college_id in (?) limit 1)", Array.wrap(cid)) }

  def college_mates
    self.class.by_college_id(self.college_ids).where("users.id != ?", id)
  end :through => :educations
end

class Education < ActiveRecord::Base
  belongs_to :user
  belongs_to :college
end

だから今あなたのコントローラーであなたは書くことができます

class WelcomesController < ApplicationController
  def index
    @posts = @current_user.college_mates.includes(:posts).map(&:posts).flatten
    # or
    @posts = Post.where(user_id: @current_user.college_mates.map(&:id))
  end
end

2番目のバリアントは3つのsql-requestを生成し、最初のバリアントは2つだけを生成します。しかし、これはデータに関する同じ作業であり、時間も同じになると思います。通常、コントローラーには数行のコードしか含まれておらず、すべてのロジックはモデルで記述されています。理想的には、コントローラーには次のものだけが含まれている必要がありますPost.by_college_mates_for(@curren_user.id)

于 2012-04-09T07:14:42.730 に答える