ruby-on-rails - 创建 has_many :through records 时跳过验证

标签 ruby-on-rails ruby-on-rails-3 validation nested-forms

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

event.rb

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations
end

user.rb
class User < ActiveRecord::Base
  has_many :participations
  has_many :events, :through => :participations
end

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

_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 %>

最佳答案

套装validate: false事件中 has_many :users协会

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations, validate: false
end

关于ruby-on-rails - 创建 has_many :through records 时跳过验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896165/

相关文章:

ruby-on-rails - 在 Rails 中执行类似操作的正确方法

ruby-on-rails - Rails 模型上的无效日期时间导致 nil

ruby-on-rails - 在不使用任何其他 gem 的情况下在 ruby​​ on rails 中开发是否很奇怪

ruby-on-rails - Rails 中带有动态内容的侧边栏的设计模式

ruby - JW Player Gem 不工作

asp.net-mvc - asp.net MVC 验证框架的选项

validation - zf2验证: How can I validate dependent fields?

ruby-on-rails - Active Admin - 用户和管理员的相同模型

ruby-on-rails - 如何在已经安装了 XAMPP 的 Ubuntu 12.10 中安装完整的 Ruby on Rails 环境?

ruby-on-rails-3 - Emacs Ruby 自动完成几乎可以工作