Rails 使用附加字段创建/更新联接 table
Rails create / update join table with additional field
加入TABLE
- 类别(外键)
- 产品(外键)
- 排名(整数)
每次create/update加入table都要填排名位置
型号
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
您需要使用具有显式模型的 has_many :association, through: :through_association
作为连接模型。这样您就可以存储有关协会本身的数据。
如 @Sean 所述,您需要使用 has_many :through
关联,因为:
You should use has_many :through
if you need validations, callbacks or extra attributes on the join model.
2.8 Choosing Between has_many :through and has_and_belongs_to_many
例如,要创建一个连接模型 rank
(我想不出比 rank 更好的名字,抱歉!):
# join table migration
class CreateRanks < ActiveRecord::Migration[5.2]
def change
create_table :ranks do |t|
t.references :product
t.references :category
t.integer :rank
t.timestamps
end
end
end
您的模特:
# product.rb
class Product < ApplicationRecord
has_many :ranks
has_many :categories, through: :ranks
end
# rank.rb
class Rank < ApplicationRecord
belongs_to :product
belongs_to :category
end
# category.rb
class Category < ApplicationRecord
has_many :ranks
has_many :products, through: :ranks
end
因此您可以按如下方式创建记录"in bulk":
Rank.create [
{ category: a, product: x, rank: 1},
{ category: b, product: y, rank: 2}
]
加入TABLE
- 类别(外键)
- 产品(外键)
- 排名(整数)
每次create/update加入table都要填排名位置
型号
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
您需要使用具有显式模型的 has_many :association, through: :through_association
作为连接模型。这样您就可以存储有关协会本身的数据。
如 @Sean 所述,您需要使用 has_many :through
关联,因为:
You should use
has_many :through
if you need validations, callbacks or extra attributes on the join model. 2.8 Choosing Between has_many :through and has_and_belongs_to_many
例如,要创建一个连接模型 rank
(我想不出比 rank 更好的名字,抱歉!):
# join table migration
class CreateRanks < ActiveRecord::Migration[5.2]
def change
create_table :ranks do |t|
t.references :product
t.references :category
t.integer :rank
t.timestamps
end
end
end
您的模特:
# product.rb
class Product < ApplicationRecord
has_many :ranks
has_many :categories, through: :ranks
end
# rank.rb
class Rank < ApplicationRecord
belongs_to :product
belongs_to :category
end
# category.rb
class Category < ApplicationRecord
has_many :ranks
has_many :products, through: :ranks
end
因此您可以按如下方式创建记录"in bulk":
Rank.create [
{ category: a, product: x, rank: 1},
{ category: b, product: y, rank: 2}
]