两个不同的类使用一个类的accepts_nested_attributes

时间:2022-11-25 08:33:31

I have a class Addresse where Organismereferent and Organisme have a has_many belongs_to relationship with. Also Organismereferent and Organisme can accepts_nested_attributes_for :addresses

我有一个类Addresse,Organismereferent和Organisme有一个has_many belongs_to关系。 Organismereferent和Organisme也可以接受_nested_attributes_for:地址

My problem is when I only had the Organismereferent class everything was working fine and I was able to create a new Organismereferent with an addresse but as soon as I created and added the same relation to Organisme , they both stopped working without giving any error message I only get this in the console: 两个不同的类使用一个类的accepts_nested_attributes

我的问题是,当我只有Organismereferent课程时,一切都运行正常,我能够创建一个新的Organismereferent与收件人,但是一旦我创建并添加了与Organisme相同的关系,他们都停止工作,而不给出任何错误消息我只能在控制台中获取:

My model:

我的模特:

class Organismereferent < ApplicationRecord
  has_many :addresses
  has_many :referents
  accepts_nested_attributes_for :addresses
end

class Organisme < ApplicationRecord
  has_many :addresses
  accepts_nested_attributes_for :addresses
end

class Address < ApplicationRecord
  belongs_to :organismereferent
  belongs_to :organisme
end

Controller for Organisme

Organisme控制器

def new
  @organisme = Organisme.new
  @organisme.addresses.build
end

def create
  @organisme = Organisme.new(organisme_params)
  @organisme.status = true
  @organisme.save
  redirect_to @organisme
end

private
  def organisme_params
    params.require(:organisme).permit(:nom, :telephone, :courriel, :fax, addresses_attributes: [:id, :no_civique, :rue, :ville, :province, :etat, :code_postal])
  end

Controller for Organismereferent:

Organismereferent控制器:

def new
  @organisme = Organismereferent.new
  @organisme.addresses.build
end

def create
  @organisme = Organismereferent.new(organisme_params)
  @organisme.active = true
  @organisme.save
  redirect_to @organisme
end

private
  def organisme_params
      params.require(:organismereferent).permit(:nom_organisation, :bureau, :telecopie, :courriel, :site_web, addresses_attributes: [:id, :no_civique, :rue, :ville, :province, :etat, :code_postal])
  end

I'm not sure what else information might be important so I'll be glad to add anything.

我不确定其他什么信息可能很重要,所以我很乐意添加任何内容。

1 个解决方案

#1


1  

Try to change address model like

尝试更改地址模型

class Address < ApplicationRecord
  belongs_to :organismereferent, optional: true
  belongs_to :organisme, optional: true
end

#1


1  

Try to change address model like

尝试更改地址模型

class Address < ApplicationRecord
  belongs_to :organismereferent, optional: true
  belongs_to :organisme, optional: true
end