创建has_many:通过记录时跳过验证

Mik*_*nov 2 validation ruby-on-rails nested-forms ruby-on-rails-3

在我的数据库中,有很多用户使用无效数据(没有手机号码).因为我刚刚添加了移动存在验证.当我尝试添加新事件时,我收到错误消息"用户移动电话号码必须是...格式".如何在创建事件时跳过用户验证?

event.rb

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations
end
Run Code Online (Sandbox Code Playgroud)

user.rb

class User < ActiveRecord::Base
  has_many :participations
  has_many :events, :through => :participations
end
Run Code Online (Sandbox Code Playgroud)

events_controller.rb

 def create
    @event = Event.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to events_path, notice: 'Event was successfully created.' }
      else
        format.html { render action: 'new' }
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

_form.html.erb

 <%= form_for @event, :html => {:class => 'row'} do |f| %>
      <%= f.error_messages %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :user_ids, "Participants" %>
      <%= f.collection_select :user_ids, User.all, :id, :name, {}, { :multiple => true } %>

      <%= clear %>

      <%= f.submit 'Save' %>
    <% end %>
Run Code Online (Sandbox Code Playgroud)

小智 7

validate: false在事件has_many :users关联中设置

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations, validate: false
end
Run Code Online (Sandbox Code Playgroud)