У меня есть два класса с отношением has_and_belongs_to_many
.
class Customer < ApplicationRecord
has_and_belongs_to_many :segments
def rematch_segments
self.segments = Segment.customer_segments(self)
end
end
class Segment < ApplicationRecord
has_and_belongs_to_many :customers
class << self
def customer_segments(customer)
...returns a collection of Segments...
end
end
end
Вызов rematch_segments
приводит к вставке для каждого сегмента.
(0.2ms) BEGIN
Customer::HABTM_Segments Create (0.8ms) INSERT INTO "customers_segments" ("customer_id", "segment_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) [["customer_id", 1], ["segment_id", 1], ["created_at", "2020-03-24 23:42:52.985400"], ["updated_at", "2020-03-24 23:42:52.985400"]]
Customer::HABTM_Segments Create (0.2ms) INSERT INTO "customers_segments" ("customer_id", "segment_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) [["customer_id", 1], ["segment_id", 2], ["created_at", "2020-03-24 23:42:52.987537"], ["updated_at", "2020-03-24 23:42:52.987537"]]
Customer::HABTM_Segments Create (0.2ms) INSERT INTO "customers_segments" ("customer_id", "segment_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) [["customer_id", 1], ["segment_id", 3], ["created_at", "2020-03-24 23:42:52.988610"], ["updated_at", "2020-03-24 23:42:52.988610"]]
(0.3ms) COMMIT
Как я могу вместо этого сделать монолитная вставка?