本文介绍了如何创建一个新的记录的许多一对多通过接受嵌套属性的关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

组织用户有很多-to-many关联到关系。起初我这实现了一个1-to-many关联,它的工作,但现在我需要它成为一个经过多次-to-many关联。所以,我创建了相关模型和模型文件改变了关联。

组织接受嵌套的用户属性在我的应用我有两个一个加盟申请表。另外,我用它在我的种子文件:

  Organization.create(名称:名称,...)!users.create(电子邮件:[email protected],...)

这工作时,这是一个1-to-many关联,但现在它是一个多一对多通过协会,它产生于无欲无求的错误:

 验证失败:成员不能为空,主持人不能为空

这是指,为相关模型变量通过用户组织相关联。

是什么原因导致这个错误;为什么这些价值观空白?是 Organization.create 行可能不正确的使用many-to-many关联? 成员主持人有默认值(见迁移文件)。我希望它创建组织用户关系使用默认值。我怎么回事,应建立一个新的组织和用户?


本组织模型:

 的has_many:关系,依赖:摧毁
的has_many:用户,通过:关系accepts_nested_attributes_for:关系:reject_if => :all_blank,:allow_destroy =>真正
validates_associated:用户

关系模式:

  belongs_to的:组织
belongs_to的:用户
accepts_nested_attributes_for:用户validates_ presence_of:组织
validates_ presence_of:用户
验证:会员,presence:真
验证:主持人,presence:真

用户模型:

 的has_many:关系,依赖:摧毁
的has_many:组织,通过:人际关系,inverse_of:用户

的关系迁移:

 类CreateRelationships< ActiveRecord的::迁移
  高清变化
    CREATE_TABLE:关系做| T |
      t.belongs_to:用户指标:真
      t.belongs_to:组织,指数:真      t.boolean:成员,空:假的,默认值:false
      t.boolean:主持人,空:假的,默认值:false
      t.timestamps空:假的
    结束
    add_index:人际关系,[:USER_ID,:organization_id],独特的:真
  结束
结束


解决方案

我觉得你的问题,也许你还没有指定一个外键为用户模型的has_many关系。尝试:

 的has_many:人际关系,foreign_key:organization_id,取决于:摧毁

这唯一标识每个关系的组织,你的用户模型。

Organization and User have a many-to-many association through Relationship. Initially I implemented this a 1-to-many association, which worked but now I need it to become a many-to-many through association. So I created the Relationship model and changed the association in the model files.

Organization accepts nested attributes for User as in my app I have a joined signup form for the two. Also, I use it in my seeds file:

Organization.create!(name: "name", ...).users.create(email: "[email protected]", ...)

This worked when it was a 1-to-many association but now it is a many-to-many through association, it produces upon seeding the error:

Validation failed: Member can't be blank, Moderator can't be blank 

This refers to variables for the Relationship model, through which User and Organization are associated.

What causes this error; why are these values blank? Is the Organization.create line perhaps incorrect for a many-to-many association? member and moderator have default values (see migration file). I would expect it to create the organization, user, and relationship with default values. How else should I be creating a new organization and user?


The Organization model:

has_many :relationships, dependent: :destroy
has_many :users, through: :relationships

accepts_nested_attributes_for :relationships, :reject_if => :all_blank, :allow_destroy => true
validates_associated :users

The Relationship model:

belongs_to :organization
belongs_to :user
accepts_nested_attributes_for :user

validates_presence_of :organization
validates_presence_of :user
validates :member, presence: true
validates :moderator, presence: true

The User model:

has_many :relationships, dependent: :destroy 
has_many :organizations, through: :relationships, inverse_of: :users

The relationship migration:

class CreateRelationships < ActiveRecord::Migration
  def change
    create_table :relationships do |t|
      t.belongs_to :user, index: true
      t.belongs_to :organization, index: true

      t.boolean :member, null: false,  default: false
      t.boolean :moderator, null: false,  default: false
      t.timestamps null: false
    end
    add_index :relationships, [:user_id, :organization_id], unique: true
  end
end
解决方案

I think your problem maybe that you haven't specified a "foreign key" for the has_many relationships in User Model. Try:

has_many :relationships, foreign_key: "organization_id", dependent: :destroy

This uniquely identifies an organization per relationship with your user model.

这篇关于如何创建一个新的记录的许多一对多通过接受嵌套属性的关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:34