Ruby on rails 创建多个:通过记录时跳过验证
在my db中,有很多用户的数据无效(没有手机号码)。因为我刚才添加了移动状态验证。当我尝试添加新事件时,会收到错误消息“Users mobile number必须为…格式”。创建事件时如何跳过用户验证 event.rbRuby on rails 创建多个:通过记录时跳过验证,ruby-on-rails,ruby-on-rails-3,validation,nested-forms,Ruby On Rails,Ruby On Rails 3,Validation,Nested Forms,在my db中,有很多用户的数据无效(没有手机号码)。因为我刚才添加了移动状态验证。当我尝试添加新事件时,会收到错误消息“Users mobile number必须为…格式”。创建事件时如何跳过用户验证 event.rb class Event < ActiveRecord::Base has_many :participations has_many :users, :through => :participations end class User < Activ
class Event < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
class User < ActiveRecord::Base
has_many :participations
has_many :events, :through => :participations
end
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_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 %>
\u form.html.erb
class Event < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
class User < ActiveRecord::Base
has_many :participations
has_many :events, :through => :participations
end
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_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 %>
{:class=>'row'}do | f |%>
真}%>
Setvalidate:false
在事件有多个:用户关联时
class Event < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations, validate: false
end
class事件:participations,验证:false
结束
我想知道如何在Rails 5.0.0.RC1中实现这一点。谢谢。创建父记录并分配子id(如Event.create(用户id:[1,2,3])
Rails在每个用户上运行valid?
,而不分配Event\u id
)。因此,您无法检测事件id是否已更改并跳过验证:(.非常感谢,这个答案对我非常有帮助。