0

現在のユーザーの属性に基づいて、ページにユーザーのリストを表示しようとしています。現在のユーザーが:position as "High School"というインスタンス変数を持っている場合は、:position "College"を持つすべてのユーザーを一覧表示します(その逆も同様です)。コントローラでifelseステートメントを使用してこれを行うことは知っていますが、クエリ呼び出しがどうあるべきかを理解できません。今私は持っています:

if current_user.position= "High School"
  @user = User.all
else
  @user= User.all

テンプレートとして。ただし、@ user =ステートメントを切り替える必要がありますが、それを制限する方法がわかりません。何か助けはありますか?ありがとう!

<% @quizzes.each do |quiz| %>
  <tr>
    <td><h6><%= quiz.userName%></h6></td>
    <td><h6><%= quiz.q1 %></h6></td>
    <td><% for q in quiz.q2 %>
      <% if q != nil %>
        <h6><%= q  %></h6>
      <% end %>
    <% end %>
    <td><h6><%= quiz.q3 %></h6></td>
    <td>X</td>
  </tr>
4

3 に答える 3

2

scope考えられる解決策の1つは、モデルクラスで使用することです。

ユーザーモデルでスコープを定義する

class User < ActiveRecord::Base
  ...

  scope :college,    where(position: 'College')
  scope :highschool, where(position: 'High School')
end

コントローラーでは、

if current_user.position == "High School"
  @users = User.college
else
  @users = User.highschool
end

それが助けになることを願っています。

于 2012-05-03T00:27:10.043 に答える
2

Rails 3:

if current_user.position == "High School"
   @user = User.where(:position => "College")
else
   @user = User.where(:position => "High School")
end

レール2:

if current_user.position == "High School"
  @user = User.find_all_by_position("College")
else
  @user = User.find_all_by_position("High School")
end
于 2012-05-03T00:29:29.703 に答える
1

おそらくこれはあなたが探しているものですか?

if current_user.position == "High School"
   @users = User.where(:position => "College")
else
   @users = User.where(:position => "High School")
end
于 2012-05-03T00:28:39.470 に答える