【问题标题】:Devise - Nested Attributes - Customer --> Customer_addresses --> Address设计 - 嵌套属性 - 客户 --> Customer_addresses --> 地址
【发布时间】:2015-05-25 09:41:18
【问题描述】:

我一直在尝试创建类似于下面列出的数据库的关系,使用 devise 创建 Customer 表。我已经使用 ID 通过表名和 ID 为其他表创建了迁移。我的模型如下所示。我知道我在 has_many 关系上做得太过火了,但我整天都在尝试这个。

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

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

我也迷失了如何在这种类型的嵌套上允许强参数,因为我能找到的大多数示例仅将它连接到包含 user_id 的表,而不是通过另一个表连接的表。

提前为指导喝彩。

DataModel image here

表格 客户 / 设计表默认值

客户地址 ID address_id address_type_id customer_id

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

地址类型 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

【问题讨论】:

    标签: ruby-on-rails-4 devise nested-attributes has-many-through


    【解决方案1】:

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

    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
    

    【讨论】:

    • 干杯。我会试试看的。
    • 仍然无法通过地址字段。但比以前更进一步。
    • 已添加表单 sn-p。如果从 user_address 表中获取 address_id,但似乎可以让它进入下一步并从地址表中提取。
    • 接近了。现在将其连接到表单并显示存储的信息:-))) 巨大的改进。只是无法保存。需要在地址表中添加一个 user_addresses_id 列。
    • 很棒的东西让它发挥作用。我会在早上通过一个完整的版本。为朝着正确的方向前进而欢呼。 :-) 试图投票,但说我需要更多代表。
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多