Search code examples
ruby-on-railsrubynested-attributes

Validate presence of nested attributes within a form


I have the following associations:

#models/contact.rb
class Contact < ActiveRecord::Base
  has_many :contacts_teams
  has_many :teams, through: :contacts

  accepts_nested_attributes_for :contacts_teams, allow_destroy: true
end

#models/contacts_team.rb
class ContactsTeam < ActiveRecord::Base
  belongs_to :contact
  belongs_to :team
end

#models/team.rb
class Team < ActiveRecord::Base
  has_many :contacts_team
  has_many :contacts, through: :contacts_teams
end

A contact should always have at least one associated team (which is specified in the rich join table of contacts_teams).

If the user tried to create a contact without an associated team: a validation should be thrown. If the user tries to remove all of a contact's associated teams: a validation should be thrown.

How do I do that?

I did look at the nested attributes docs. I also looked at this article and this article which are both a bit dated.

For completion: I am using the nested_form_fields gem to dynamically add new associated teams to a contact. Here is the relevant part on the form (which works, but currently not validating that at least one team was associated to the contact):

<%= f.nested_fields_for :contacts_teams do |ff| %>
  <%= ff.remove_nested_fields_link %>
  <%= ff.label :team_id %>
  <%= ff.collection_select(:team_id, Team.all, :id, :name) %>
<% end %>
<br>
<div><%= f.add_nested_fields_link :contacts_teams, "Add Team"%></div>

So when "Add Team" is not clicked then nothing gets passed through the params related to teams, so no contacts_team record gets created. But when "Add Team" is clicked and a team is selected and form submitted, something like this gets passed through the params:

"contacts_teams_attributes"=>{"0"=>{"team_id"=>"1"}}

Solution

  • This does the validations for both creating and updating a contact: making sure there is at least one associated contacts_team. There is a current edge case which leads to a poor user experience. I posted that question here. For the most part though this does the trick.

    #custom validation within models/contact.rb
    class Contact < ActiveRecord::Base
      ...
      validate :at_least_one_contacts_team
    
      private
      def at_least_one_contacts_team
        # when creating a new contact: making sure at least one team exists
        return errors.add :base, "Must have at least one Team" unless contacts_teams.length > 0
    
        # when updating an existing contact: Making sure that at least one team would exist
        return errors.add :base, "Must have at least one Team" if contacts_teams.reject{|contacts_team| contacts_team._destroy == true}.empty?
      end           
    end