问题
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.
回答1:
The has_many :through Association
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
来源:https://stackoverflow.com/questions/53443147/how-can-i-insert-or-store-multiple-ids-in-a-single-column-in-rails