Participant への属している関連付けを持つモデル サブスクリプションがあります。
サブスクリプション フォームは fields_for を使用して、関連する参加者フィールドを作成します。
また、フォームには「other_person」というラジオ ボタンがあります。
私が望むのは、other_person フィールドが false に設定されている場合、関連付けられた参加者テーブルを保存しないことです (したがって、検証も行いません)。
Participant への属している関連付けを持つモデル サブスクリプションがあります。
サブスクリプション フォームは fields_for を使用して、関連する参加者フィールドを作成します。
また、フォームには「other_person」というラジオ ボタンがあります。
私が望むのは、other_person フィールドが false に設定されている場合、関連付けられた参加者テーブルを保存しないことです (したがって、検証も行いません)。
次の例では、 がモデルother_person
のフィールドであると仮定します。Subscription
class Subscription < ActiveRecord::Base
before_save :remove_empty_participant
belongs_to :participant
private
def remove_empty_participant
self.participant = nil unless self.other_person
end
end
モデルのフィールドでない場合Subscription
は、コントローラーのアクションで属性を削除する必要があります。
class SubscriptionsController < ActionController
def create
params[:subscription].delete(:participant) unless params[:other_person]
# Save the subscription with your current params...
end
end
それが役に立てば幸い。