0

我有一個關於我有一個協會的創建語句的問題。我對更新沒有任何問題,但是當我使用簡單的窗體關聯創建新對象時,我在通過模型上的驗證將所有事情都搞定了。嵌套有很多通過簡單的表單多選擇驗證

形式

%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 

我得到的錯誤是「user_groups.group_id」=> [「不能爲空」],我敢肯定它是由我的user_groups模型中的驗證引起的。

有什麼建議嗎?我試着按照這裏的指南:http://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through但他們並沒有真正做出多選擇他們的選擇。不知道什麼方向來到這裏,這是奇怪的,我不需要改變任何東西,它的作品更新完美...

回答

0

修復它!

Inverse_of是一個非常奇怪的事情,我並沒有真正明白,但這是非常有價值和強大的。基本上ActiveRecord不明白兩個對象相互引用。舉例來說,如果你有一個關係,即1-1所示:

類測試 的has_many:回答 結束

類答 的has_many:測試 結束

測試及答案不會沒有數據庫查詢,他們引用相同的東西。反過來告訴Rails他們沒有這個。很酷的東西。

我的溶液加入 的has_many:user_groups,:inverse_of =>:組

到組

的has_many:user_groups,:inverse_of =>:用戶

爲了用戶和一切工作。

神奇。

還刪除了user_group模型上對id的引用,以驗證對象的存在。