Daw*_*een 6 mysql activerecord ruby-on-rails insert has-many
我有三个模型:
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
Run Code Online (Sandbox Code Playgroud)
我通读了一个 CSV 文件来创建优惠券和coupon_events。这是非常低效的,因为一次创建一个记录并导致多个查询,每个查询包括两个插入语句。
我想使用这样的单个插入查询:
coupon_string = " ('abc','AAA'), ('123','BBB')"
Coupon.connection.insert("INSERT INTO coupons (code, name) VALUES"+coupon_string)
Run Code Online (Sandbox Code Playgroud)
然后我需要为 CouponEvent 模型创建第二个插入查询,但我需要一个返回的coupon_ids 的列表。是否有内置方法可以在插入时检索 ID?
ror*_*rra -2
如果您使用 mysql 并且没有在另一个脚本/进程中插入更多行,则可以使用 last_insert_id() 获取插入的第一行的 id
first_id = ActiveRecord::Base.connection.execute("select last_insert_id()").first[0]
Run Code Online (Sandbox Code Playgroud)
然后依次生成其他记录的id。
IE
data = %w(one two three)
to_insert = "('" + data.join("'), ('") + "')"
Model.connection.insert("INSERT INTO models(name) VALUES #{to_insert}")
first_id = ActiveRecord::Base.connection.execute("select last_insert_id()").first[0].to_i
hash = {}
data.each_with_index {|d, i| hash[first_id + i] = d}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4280 次 |
| 最近记录: |