Skip to content

Instantly share code, notes, and snippets.

@adavia
Last active January 31, 2017 16:17
Show Gist options
  • Select an option

  • Save adavia/243eb71d50444b4f125aa24d4f685d63 to your computer and use it in GitHub Desktop.

Select an option

Save adavia/243eb71d50444b4f125aa24d4f685d63 to your computer and use it in GitHub Desktop.
Reject based on deep nested blank attributes
class Answer < ApplicationRecord
has_one :answer_open, dependent: :destroy
has_one :answer_rating, dependent: :destroy
accepts_nested_attributes_for :answer_open, reject_if: :all_blank
accepts_nested_attributes_for :answer_rating, reject_if: :all_blank
end
class Submission < ApplicationRecord
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers, reject_if: proc { |attributes|
attributes.all? do |key, value|
if value.is_a? ActionController::Parameters
value.all? { |nested_key, nested_value| nested_key == '_destroy' || nested_value.blank? }
else
key == '_destroy' || value.blank?
end
end
}
# REJECT ANSWER IF ASSOCIATED ANSWER_TYPE IS BLANK
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment