私は3つのモデルを持っています:
class Coupon < ActiveRecord::Base
belongs_to :event
has_many :coupon_events, :dependent => :destroy
has_many :events, :through => :coupon_events
end
class Event < ActiveRecord::Base
belongs_to :event
has_many :coupon_events, :dependent => :destroy
has_many :coupons, :through => :coupon_events
end
class CouponEvent < ActiveRecord::Base
belongs_to :coupon
belongs_to :event
end
CSVファイルを読んでクーポンとcoupon_eventsを作成しました。レコードは一度に1つずつ作成され、それぞれ2つの挿入ステートメントを含む複数のクエリが発生するため、これは非常に非効率的です。
次のような単一の挿入クエリを使用したいと思います。
coupon_string = " ('abc','AAA'), ('123','BBB')"
Coupon.connection.insert("INSERT INTO coupons (code, name) VALUES"+coupon_string)
次に、CouponEventモデルの2番目の挿入クエリを作成する必要がありますが、返されたcoupon_idのリストが必要です。挿入時にIDを取得するための組み込みメソッドはありますか?