依赖数据库来创建ID,因此它们不必是连续的。db中的自动增量功能不会因为多线程而被破坏。你可以使用activerecord导入的标准fork,我的PR上周被接受了。MySQL不支持返回的吗? class Coupon < ActiveRecord::
依赖数据库来创建ID,因此它们不必是连续的。db中的自动增量功能不会因为多线程而被破坏。你可以使用activerecord导入的标准fork,我的PR上周被接受了。MySQL不支持返回的吗? class Coupon < ActiveRecord::,mysql,ruby-on-rails,activerecord,insert,has-many,Mysql,Ruby On Rails,Activerecord,Insert,Has Many,依赖数据库来创建ID,因此它们不必是连续的。db中的自动增量功能不会因为多线程而被破坏。你可以使用activerecord导入的标准fork,我的PR上周被接受了。MySQL不支持返回的吗? class Coupon < ActiveRecord::Base belongs_to :event has_many :coupon_events, :dependent => :destroy has_many :events, :through => :coupon_e
依赖数据库来创建ID,因此它们不必是连续的。db中的自动增量功能不会因为多线程而被破坏。你可以使用activerecord导入的标准fork,我的PR上周被接受了。MySQL不支持返回的吗?
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
coupon_string = " ('abc','AAA'), ('123','BBB')"
Coupon.connection.insert("INSERT INTO coupons (code, name) VALUES"+coupon_string)
new_coupons = Coupon.create([
{ :code => "abc", :name => "AAA" },
{ :code => "123", :name => "BBB" }
])
CouponEvent.create([
{ :enevt_id => ..., coupon_id: ...},
...
])
res = Coupon.connection.execute(<<-EOSQL)
INSERT INTO coupons (code, name)
VALUES #{values}
RETURNING id, code
EOSQL
res.map {|row|
{ :coupon_id => row["id"],
:event_id => events.find { |e| e.coupon_code == row["code"] }
}
}
first_id = ActiveRecord::Base.connection.execute("select last_insert_id()").first[0]
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}
insert_sql = <<-SQL
WITH inserted_ids AS (
INSERT INTO clients (email, name) VALUES #{array.join(', ')}
RETURNING id
)
SELECT * FROM inserted_ids
SQL
result = ActiveRecord::Base.connection.execute(insert_sql)