2012-04-28 8 views
1

私はインストラクターの支払いを記録するフォームを持っています。支払は支出テーブルに記録されます。Rails 3. grouped_collection_selectからレコードを除外する方法は?

<%= f.grouped_collection_select :scheduled_session_id, User.where(:role => 'instructor'), :scheduled_sessions, :full_name, :id, :identifier_with_cost, :include_blank => true %> 

講師名と割り当てられたセッションの選択メニューが表示されます。たとえば...

Peter Parker 
    Biology $350 April 27 
    Chemistry $400 April 28 
Professor X 
    Genetics $400 April 27 
    History $350 April 28 
    Literature $350 April 29 

しかし、すでに支払っているセッションを除外したいと思います。 scheduled_sessionsテーブルには、 "paid"というブール型列があります。

ScheduledSession.where(paid: [false, nil])でクエリを作成できますが、grouped_collection_selectに実装する方法はありますか?

答えて

2

脳をコーディングしている間に多くの情報を処理することができないので、私は今週のポモドーロ技術を実装している理由を知っています。grouped_collection_select(object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) public、それは提供するgroup_methodです子レコードのリスト

この解決策を考え出すために、私は経費モデルを少し無視します。私は今、UserとScheduledSessionモデルを気にしています。なぜなら、それが私の選択メニューを生成するからです。

f.grouped_collection_select :scheduled_session_id, User.where(:role => 'instructor'), :not_paid_scheduled_sessions, :full_name, :id, :identifier_with_cost 

が、それはあまりにも他の誰かがお役に立てば幸いです...

class User < ActiveRecord::Base 
has_many :scheduled_sessions, :foreign_key => 'instructor_id' 

def not_paid_scheduled_sessions 
    ScheduledSession.where(:instructor_id => self.id).where(paid: [false, nil]) 
end 

ので、代わりの...私が使用

f.grouped_collection_select :scheduled_session_id, User.where(:role => 'instructor'), :scheduled_sessions, :full_name, :id, :identifier_with_cost, :include_blank => true 

:私は親モデルにメソッドを作成しました。

関連する問題