Validate presence of nested attributes within a fo

2019-05-06 06:23发布

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"}}

4条回答
叼着烟拽天下
2楼-- · 2019-05-06 06:36

In Rails 5 this can be done using:

validates :contacts_teams, :presence => true
查看更多
虎瘦雄心在
3楼-- · 2019-05-06 06:48

If you have a Profile model nested in a User model, and you want to validate the nested model, you can write something like this: (you also need validates_presence_of because validates_associated doesn't validate the profile if the user doesn't have any associated profile)

class User < ApplicationRecord

  has_one :profile
  accepts_nested_attributes_for :profile
  validates_presence_of :profile
  validates_associated :profile
查看更多
贼婆χ
4楼-- · 2019-05-06 06:50

docs recommend using reject_if and passing it a proc:

accepts_nested_attributes_for :posts, reject_if: proc { |attributes| attributes['title'].blank? }

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

查看更多
老娘就宠你
5楼-- · 2019-05-06 06:54

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
查看更多
登录 后发表回答