Rails HABTM 设置、模型对象和 join_table 插入控制器设置

2024-06-20

我有以下设置。

1 个产品有多个 Product_types。 许多 Product_types 有 1 种类型。 根据我对文档的理解,HABTM 关系。

我的模型是

class Product < ApplicationRecord
  has_and_belongs_to_many :types
end

class Type < ApplicationRecord
  has_and_belongs_to_many :products
end

我有一个这样的连接表迁移

class CreateJoinTableProductTypes < ActiveRecord::Migration[5.1]
  def change
    create_join_table :products, :types do |t|
      t.index :product_id
      t.index :type_id
    end
  end
end

我已经创建了一个表单 - 希望创建正确,现在我在表单提交上发送了以下参数:

"product"=>{"name"=>"Product A", "description"=>"A cool product", "image_dir_path"=>"./",
"type"=>{"id"=>"1"}},"commit"=>"Create Product"}

我在想 1)在表单和控制器中提交创建产品的参数的最佳/rails约定是什么?

and

2)我如何获得插入到连接表中的记录?

我有以下获取参数的方法

def product_params
  params.require(:product).permit(:name, :description, :image_dir_path, :type,)
end

但即便如此,我也可以在日志中看到 :type 的未经允许的参数

目前我的控制器只有:

@product = Product.new(product_params)

我非常感谢任何有关创建该对象的 Rails 方式的建议。我已经阅读了 HABTM 的 api 文档,但在涉及模型对象或我应该如何在控制器中处理这些东西时没有看到任何内容。

Thanks!


ActieRecord 生成_ids所有人的 setter 和 getterhas_many http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many and has_and_belongs_to_many http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many协会。

# creates 3 rows in products_types if they do not exist
# also deletes any rows not in the array
@product.update(type_ids: [1,2,3])

这些与一起使用表单选项助手 http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models分配关联:

<%= form_for(@product) do |f| %>
  <div class="field">
    <%= f.label :type_ids %>
    <%= f.collection_select :type_ids, Type.all, :id, :name, multiple: true %>
  </div>
  ...
<% end %>

要将参数列入白名单,请将其作为带有值的选项传递[]它允许包含任何标量类型的数组。

def product_params
  params.require(:product).permit(:name, :description, :image_dir_path, type_ids: [])
end

2)我如何获得插入到连接表中的记录?

随着has_and_belongs_to_many关联,您只能间接插入/访问行。

例如通过:

@product.types 
# or
@product.types << Type.first
# or
@product.types.create(name: 'Foo')

或者通过使用type_ids前面提到过 setter/getter。这是与has_many through:其中您有一个可以直接查询或创建的连接模型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails HABTM 设置、模型对象和 join_table 插入控制器设置 的相关文章

随机推荐