Skip to content

Instantly share code, notes, and snippets.

@cpruitt
Forked from adavia/answer.rb
Last active January 31, 2017 16:25
Show Gist options
  • Select an option

  • Save cpruitt/ec1a9095020ea042b735b2ba760a8a5d to your computer and use it in GitHub Desktop.

Select an option

Save cpruitt/ec1a9095020ea042b735b2ba760a8a5d 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|
answer_open_attrs = attributes["answer_open_attributes"]
return false unless answer_open_attrs && answer_open_attrs["response"]
true
}
# 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