Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JamesAndresCM/0adfef24c2621fa3d81d68653caa139f to your computer and use it in GitHub Desktop.
Save JamesAndresCM/0adfef24c2621fa3d81d68653caa139f to your computer and use it in GitHub Desktop.
class ReviewConfiguration < BaseRecord
has_many :review_answers, dependent: :destroy
accepts_nested_attributes_for :review_answers, allow_destroy: true
end
def review_answers_attributes=(attributes)
attributes = attributes.map(&:deep_symbolize_keys)
review_answer_ids = attributes.map{|el| el[:id]}.compact
begin
review_answers << ReviewAnswer.where(id: review_answer_ids)
super attributes
rescue ActiveRecord::RecordNotFound
false
end
end
# in controller
def update
review = begin
@review_configuration.update(review_configuration_params)
rescue ActiveRecord::RecordNotFound
record = params["review_configuration"]["review_answers_attributes"].map{|el| el["id"]}
render json: {msg: "alguno de estos registros: #{record} no existen"} and return
end
if review
render status: :ok, json: render_review_configuration(@review_configuration)
else
render status: :unprocessable_entity, json: { msg: @review_configuration.errors }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment