0

カスタム スコープで grouped_selects group_method を使用しようとしています。ユーザーは、自分が属しているプロジェクトとタスクのみを表示できます。

これは機能しています。選択するタスクを含むすべてのプロジェクトを取得しました:

# using simple_form
<%= f.input :project_id, :as => :grouped_select,
            :collection => Project.my_scope(current_user),
            :group_method => :tasks %>

これは機能していません。my_scope からタスクを取得しようとしています。

# using simple_form
<%= f.input :project_id, :as => :grouped_select,
            :collection => Project.my_scope(current_user),
            :group_method => Task.my_scope(current_user) %>

アップデート

Railsのデフォルトヘルパーでもこれを試しましたが、これはうまくいくようです:

<%= f.grouped_collection_select(:project_id,
                                Project.my_scope(current_user),
                                :"tasks.my_scope(#{current_user.id})",
                                :name, :id, :name) %>

これは一般的な方法ですか、それとも私のニーズを満たす他の方法はありますか?

4

1 に答える 1

3

:group_method呼び出すメソッドを指定し、選択したものを返すものでグループ化するため、機能していません

したがって、スコープはそこで使用できません。どのような結果を達成したいですか?おそらく、コレクションを制限することができます-current_userのタスクを持つプロジェクトのみを表示したい場合(想定)次に、このようにsmthを実行できます

# using simple_form
<%= f.input :project_id, :as => :grouped_select,
            :collection => Project.my_scope(current_user).joins(:tasks).where(:assigned_to => current_user),
            :group_method => :task %>
于 2012-05-03T11:04:02.103 に答える