ruby - Rails: Validate presence of associations in a many-to-many relation -


i trying validate presence of clients in user class, seems not working. never pass. want create user has many clients:

 class user < activerecord::base   has_many :client_users, dependent: :destroy  has_many :clients, through: :client_users  accepts_nested_attributes_for :client_users    validates_presence_of :name  validates_presence_of :clients    end 

.....

class clientuser < activerecord::base  belongs_to :client belongs_to :user  validates_presence_of :client validates_presence_of :user  accepts_nested_attributes_for :user accepts_nested_attributes_for :client  self.table_name = "clients_users"  end 

.....

class client < activerecord::base has_many :person_contacts  has_many :users, through: :client_users has_many :client_users, dependent: :destroy  has_many :cases, through: :client_cases has_many :client_cases, dependent: :destroy  accepts_nested_attributes_for :client_users, :person_contacts,  allow_destroy: true  validates_presence_of :name  end 

....

part of view:

= fields_for :clients |c|         = c.select :id, client.all.collect { |c| [c.name, c.id] }, {},  class: "select2-multiple user-clients form-control", multiple: "multiple" 

and paramaters processing users controller:

parameters: {"utf8"=>"✓",   "authenticity_token"=>"d33yucerxteatlx4tova==", "user"=>{"name"=>"", "surname"=>"", "email"=>"", "password"=>"[filtered]", "password_confirmation"=>"[filtered]", "address_city"=>"", "address_street"=>"", "address_house_number"=>"", "address_local_number"=>"", "address_postal_code"=>"", "address_postal_code_place"=>"", "phone"=>"", "role_id"=>"4"}, "clients"=>{"id"=>["", "2"]}, "commit"=>"utwórz użytkownika"} 

everything work without validation, need ensure user pass clients in users form

use custom validation

validate :has_clients  def has_clients      errors.add(:base,  "there have clients") unless clients.size > 0 end 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -