Last active
January 31, 2017 16:17
-
-
Save adavia/243eb71d50444b4f125aa24d4f685d63 to your computer and use it in GitHub Desktop.
Reject based on deep nested blank attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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