Emmanuel Mtali Emmanuel Mtali - 1 year ago 57
Ruby Question

How to validate presence of at least one nested object?


class Survey < ApplicationRecord
has_many :questions, inverse_of: :survey, :dependent => :destroy
accepts_nested_attributes_for :questions
validates_associated :questions

class Question < ApplicationRecord
belongs_to :survey, inverse_of: :questions
validates_presence_of :survey

My Surveys Controller

def new
@survey = Survey.new
2.times {@survey.questions.build}


<%= form_for @survey do |f|%>
<%= f.label :name%>
<%= f.text_field :name%>
<%= f.fields_for :questions do |builder|%>
<%= builder.text_area :content, rows: 3%>
<% end %>

<p><%= f.submit %></p>

<% end %>

As you can see when user creates a survey the form provides two questions, i want user to supply at least one question when creating the survey. How can it be achieve???

Answer Source

One of the options is to use custom validation:

validate :questions_count


# or something more explicit, like `at_least_one_question` (credits to @MrYoshiji)
def questions_count
    'You can not save a survey without questions. Add at least one question'
  ) if questions.none?

Basically, the validation will be fired every time you create or "touch" (update) the survey object, and it will fail, if survey will not have at least one question associated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download