I have three models:
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
I read through a CSV file to create coupons and coupon_events. This is terribly inefficient since the records are created one at a time and result in multiple queries each including the two insert statements.
I'd like to use a single insert query like this:
coupon_string = " ('abc','AAA'), ('123','BBB')"
Coupon.connection.insert("INSERT INTO coupons (code, name) VALUES"+coupon_string)
I then need to create the second insert query for the CouponEvent model, but I need a list of the returned coupon_ids. Is there a built in method to retrieve the IDs at the time of the insert?