设计 - 嵌套属性 - 客户 --> Customer_addresses --> 地址

Devise - Nested Attributes - Customer --> Customer_addresses --> Address

我一直在尝试创建类似于下面列出的数据库的关系,使用设计来创建客户 table。我已经使用 ID 通过 table-名称和 ID 为其他 table 创建了迁移。我的模型如下所示。我知道我在 has_many 关系上过火了,但我一整天都在尝试这个。

任何人都可以帮助或展示正确的设置方法,以便我可以创建、编辑和更新用户的地址。我已经通过简单的嵌套属性取得了成功,比如 Customer/Direct to address 但是当我将 table 放在中间时,我无法显示或更新地址属性。

我想还有一个更复杂的编辑、创建、销毁方法也需要实现。

我也迷失了如何在这种类型的嵌套上允许强参数,因为我能找到的大多数例子只将它连接到 tables,其中包含 user_id 并且不是通过另一个 table 连接的。

提前为指导喝彩

DataModel image here

表格 客户/设计table默认值

customer_addresses ID address_id address_type_id customer_id

地址 ID 地址 xzy Marua 路 其他细节/前屋

address_type ID address_type / 家庭、企业等 address_type_description / 你住在哪里等等

class Customer < ActiveRecord::Base
has_many :customer_addresses
has_many :addresses
has_many :address_types

accepts_nested_attributes_for :customer_addresses
accepts_nested_attributes_for :address_types
accepts_nested_attributes_for :addresses
end

class UserAddress < ActiveRecord::Base
has_many :customer
has_many :address_types
has_many :addresses
end

class AddressType < ActiveRecord::Base
belongs_to :customer_address
end

class Address < ActiveRecord::Base
belongs_to :user_address
end

  <div class="field">
    <%= f.fields_for :user_addresses do |ff| %>
        <div>
          <%= ff.label :address_id %><br />
          <%= ff.text_field :address_id %>
            <%= ff.fields_for :address do |fff| %>
              <%= fff.label :address %><br />
              <%= fff.text_field :address %>
          <% end %>
    <% end %>
        </div>
    </div>

user controller params

 def user_params
params.require(:user).permit(:id, :username, :first_name, :last_name, :email, :password, :password_confirmation,
                            user_address_attributes:[:user_is, :address_id, :address_type_id],
                            addresses_attributes:[:id, :address, :other_address_details ],
                            address_type_attributes:[ :id, :address_type])
  end

您定义的关联应该如下所示

class Customer
  has_many :customer_addresses
  has_many :addresses, :through => :customer_addresses
  has_many :address_types, :through => :customer_addresses
end

class Address
  has_many :customer_addresses
  has_many :customers, :through => :customer_addresses
  has_many :address_types, :through => :customer_addresses
end

class AddressType
  has_many :customer_addresses
  has_many :customers, :through => :customer_addresses
  has_many :addresses, :through => :customer_addresses
end

class CustomerAddress
  belongs_to :customer
  belongs_to :address
  belongs_to :address_type
end
class Customer < ActiveRecord::Base

  has_many :customer_addresses
  accepts_nested_attributes_for :customer_addresses

end

class AddressType
  has_many :customer_addresses

end

class CustomerAddress

 belongs_to :address_type
 belongs_to :customer
 belongs_to :address

 accepts_nested_attributes_for :address

 after_initialize :add_address, unless: 'address.present?'

 def add_address
    self.build_address
 end 

end