【发布时间】:2019-02-25 22:42:42
【问题描述】:
加入表格
- 类别(外键)
- 产品(外键)
- 排名(整数)
每次创建/更新时间连接表时,我都必须插入排名位置。
型号
class Category < ApplicationRecord
has_and_belongs_to_many :products
class Product < ApplicationRecord
has_and_belongs_to_many :categories
Schema.db
create_table "products_categories", id: false, force: :cascade do |t|
t.bigint "category_id", null: false
t.bigint "product_id", null: false
t.integer "rank"
t.index ["category_id", "product_id"], name: "index_products_categories_on_category_id_and_product_id"
end
我知道我能做到。但是如何传递排名值?
c = Category.find(1)
c.products = array_of_products
c.save
Rails 5.2
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-5 rails-activerecord