深度嵌套_属性和模型之间的关系

时间:2017-11-27 22:15:43

标签: ruby-on-rails ruby relationship nested-attributes

我有4个型号。 用户有重定位和宠物。 宠物和重新定位通过Crate模型具有关系has_many。 当我创建重定位时,我使用带有Crates的nested_attributes,并进入每个Crate - Pet。 通过设计通过current_user帮助程序完成。并且用户模型不会获得user_id。

mongoengine.connect(
    db=DB_NAME,
    host="127.0.0.1",
    port=server.local_bind_port
)

Api请求看起来像

    class Relocation < ApplicationRecord
      belongs_to :user
      has_many :crates
      has_many :pets, through: :crates
      accepts_nested_attributes_for :crates
    end

    class User < ApplicationRecord
      has_many :relocations
      has_many :pets
    end

    class Pet < ApplicationRecord
      belongs_to :user
      has_many :crates
      has_many :relocations, through: :crates
    end

    class Crate < ApplicationRecord
      belongs_to :pet
      belongs_to :relocation
      accepts_nested_attributes_for :pet
    end

当我尝试创建重定位时,我收到有关&#39; no user_id&#39;为宠物模型。

这是方式,我如何链接深嵌套宠物与用户。有更好的想法吗?

    relocation: {crate_attributes: {pets_attributes: {}}}

0 个答案:

没有答案