通过简单表单多选验证,嵌套有很多

时间:2014-10-30 00:11:46

标签: ruby-on-rails validation activerecord simple-form has-many-through

我遇到了与我有关联的create语句的问题。我对更新没有任何问题,但是当我使用简单的表单关联创建一个新对象时,我对通过模型的验证会搞砸了所有内容。

表格

%div.inner-padding
= simple_form_for(@group, :wrapper => :bootstrap3_horizontal, :html => { multipart: true, :class => 'form-horizontal' }) do |f|
  %section#group-show
    %div.panel
      %div
        = f.input :name
        = f.association :group_type, collection: current_user.organization.group_types.all, required: true unless current_user.organization.nil?
        = create_link(:group_type, 'Create a new group type')
        -if can? :access, @group, :organization
          = f.association :organization, required: true
      %div.group-users
        %div.group-users-header
          %h3 Users
          %hr
        %div
          = f.association :users, collection: current_user.organization.users.all, as: :check_boxes, label_method: :basic_info,
                          include_blank: false, label: false, class:'list-group-item' unless current_user.organization.nil?

模型

class Group < ActiveRecord::Base
  belongs_to :group_type
  belongs_to :organization

  has_many :user_groups, inverse_of: :group

  has_many :users, through: :user_groups,
           :after_remove    => :alter_user,
           :after_add       => :alter_user,
           inverse_of: :groups

  accepts_nested_attributes_for :user_groups, allow_destroy: true

...

class UserGroup < ActiveRecord::Base
  before_save :process_expirations unless :skip_callbacks

  belongs_to :user
  belongs_to :group,  -> { includes :training_types }

  accepts_nested_attributes_for :user

  # # # # # # # # # # # # # # #
  # Validations
  # # # # # # # # # # # # # # #

  validates :user_id,
            :group_id,
            :presence => true

  validates_uniqueness_of :user_id, :scope => :group_id

class User < ActiveRecord::Base
  rolify

  devise :invitable, :database_authenticatable,
         :recoverable, :rememberable, :trackable

  validates :password, :presence => true,
            :confirmation => true,
            :length => {:within => 6..40},
            :on => :create
  validates :password, :confirmation => true,
            :length => {:within => 6..40},
            :allow_blank => true,
            :on => :update

  # # # # # # # # # # # # # # #
  # Relations
  # # # # # # # # # # # # # # #

  has_many :user_groups
  has_many :groups, through: :user_groups,
           :after_remove     => :alter_group,
           :after_add        => :alter_group,
           :dependent        => :destroy

  has_many :training_histories, -> { includes :lecture }

  has_many :training_types, through: :expirations,
           :after_remove             => :alter_training_type,
           :after_add                => :alter_training_type

控制器操作

  def new
    @group = Group.new
    @group.user_groups.build.build_user
    add_breadcrumb 'new group', new_group_path
  end

我得到的错误是&#34; user_groups.group_id&#34; =&gt; [&#34;不能为空白&#34;]而且我很确定它的确如此由我的user_groups模型中的验证引起的。

有什么建议吗?我尝试按照指南进行操作:http://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through但他们并没有真正为他们的选项做多选。不知道要到这里的方向是什么,我不必改变任何东西并且它完全适用于更新,这很奇怪......

1 个答案:

答案 0 :(得分:0)

修正了它!

Inverse_of是一个非常奇怪的事情,我显然并不是很清楚,但这是非常有价值和强大的。基本上ActiveRecord不理解两个对象相互引用。例如,如果您的关系是1-1,那么:

班级考试 has_many:答案 端

班级答案 has_many:测试 端

如果没有数据库查询,测试和答案将无法识别它们引用相同的内容。反向告诉Rails他们没有那个。很酷的东西。

我的解决方案是添加 has_many:user_groups,:inverse_of =&gt; :组

分组

has_many:user_groups,:inverse_of =&gt; :用户

To User和一切正常。

魔法。

还删除了user_group模型上对id的引用,以仅验证对象的存在。