Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
依赖数据库来创建ID,因此它们不必是连续的。db中的自动增量功能不会因为多线程而被破坏。你可以使用activerecord导入的标准fork,我的PR上周被接受了。MySQL不支持返回的吗? class Coupon < ActiveRecord::_Mysql_Ruby On Rails_Activerecord_Insert_Has Many - Fatal编程技术网

依赖数据库来创建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)