0
我有Company
和Product
模型與has_and_belongs_to_many
的關係。HABTM關係複選框可以添加更多選項
# app/models/company.rb
class Company < ApplicationRecord
has_and_belongs_to_many :products
end
# app/models/product.rb
class Product < ApplicationRecord
has_and_belongs_to_many :companies
end
我想爲產品製作帶有複選框的公司編輯表單,並且還可以選擇添加新產品。上面列出
6種產品爲默認產品的所有公司。
添加了其他字段的產品只會顯示給添加它們的公司,如下所示。
問題: 我不能添加其他公司的具體產品,我目前的HABTM關聯,我想知道的軌道的方式來實現這一目標。請幫忙!
你可以使用'Product.pluck獲取產品(:名稱)' –
我的問題是:我如何能更多的產品爲公司通過其他的輸入欄後顯示所有公司的默認產品? –