问题描述
如何将多个ID插入或保存为逗号分隔的(例如(2,5,8,10)值)在数据库中的多列关系中?我正在使用主动管理员进行资源管理。
How can I insert or save multiple ids as comma separated e.g (2,5,8,10) values in single column in database for many to many relationship? I am using active admin for resource management.
推荐答案
has_many:through关联
The has_many :through Association
has_many:through关联通常用于与另一个模型建立多对多连接。这种关联表明,通过进行第三个模型,可以将声明模型与另一个模型的零个或多个实例匹配。例如,考虑一种医疗实践,患者要预约看医生。相关的协会声明可能看起来像这样
A has_many :through association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this
class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end
这篇关于如何在Rails的单个列中插入或存储多个ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!