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

jim*_*gug 0 controller join model ruby-on-rails has-and-belongs-to-many

我有以下设置。

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

"product"=>{"name"=>"Product A", "description"=>"A cool product", "image_dir_path"=>"./",
"type"=>{"id"=>"1"}},"commit"=>"Create Product"}
Run Code Online (Sandbox Code Playgroud)

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

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

我有以下获取参数的方法

def product_params
  params.require(:product).permit(:name, :description, :image_dir_path, :type,)
end
Run Code Online (Sandbox Code Playgroud)

但即使这样我也可以在日志中看到 :type 的未经允许的参数

目前我的控制器只有:

@product = Product.new(product_params)
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

max*_*max 5

ActieRecord 为所有关联生成_idssetter 和 getter 。has_manyhas_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])
Run Code Online (Sandbox Code Playgroud)

它们与表单选项帮助程序一起使用来分配关联:

<%= 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 %>
Run Code Online (Sandbox Code Playgroud)

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

def product_params
  params.require(:product).permit(:name, :description, :image_dir_path, type_ids: [])
end
Run Code Online (Sandbox Code Playgroud)

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

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

例如通过:

@product.types 
# or
@product.types << Type.first
# or
@product.types.create(name: 'Foo')
Run Code Online (Sandbox Code Playgroud)

或者使用type_ids前面提到的 setter/getter。has_many through:这是与可以直接查询或创建的连接模型的关键区别。