Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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/1/typo3/2.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
Ruby on rails Rails为同一条记录创建多个令牌(一次提供多个优惠券)_Ruby On Rails_Ruby_Ruby On Rails 4_Multiple Records - Fatal编程技术网

Ruby on rails Rails为同一条记录创建多个令牌(一次提供多个优惠券)

Ruby on rails Rails为同一条记录创建多个令牌(一次提供多个优惠券),ruby-on-rails,ruby,ruby-on-rails-4,multiple-records,Ruby On Rails,Ruby,Ruby On Rails 4,Multiple Records,下面是我想做的: 当用户创建特别优惠时,他会写下应该有多少优惠券。每个优惠券都以唯一代码存储在数据库中 例如: 他想卖100张优惠券作为“晚餐优惠”。当他创建“晚餐优惠”并输入他想要100张优惠券时,应该生成这100张优惠券。 我该怎么做 谢谢。我假设您已经有了带有关系的优惠模型,并且您的优惠模型具有属于:优惠关系 如果是这样的话,那你就可以做了 amount_of_coupons = params[:amount_of_coupons] offer = Offer.create params.

下面是我想做的: 当用户创建特别优惠时,他会写下应该有多少优惠券。每个优惠券都以唯一代码存储在数据库中

例如:

他想卖100张优惠券作为“晚餐优惠”。当他创建“晚餐优惠”并输入他想要100张优惠券时,应该生成这100张优惠券。 我该怎么做


谢谢。

我假设您已经有了带有关系的优惠模型,并且您的优惠模型具有属于:优惠关系

如果是这样的话,那你就可以做了

amount_of_coupons = params[:amount_of_coupons]
offer = Offer.create params.permit(:attr1, :attr2)
offer.coupons.create Array.new(amount_of_coupons).map{ {code: (0...64).map{ (65 + rand(26)).chr }.join} }

这将创建100个连接到您的优惠的优惠券,随机生成64个字符的代码。

在我看来,最好让优惠券本身生成优惠券代码,这样可以使整个系统的代码长度相同

amount_of_coupons = 100

amount_of_coupons.times do
    offer.coupons.create({:attr => value})
end

class Coupon < ActiveRecord::Base
    attr_accessible :code
    before_create :set_coupon_code

    def set_coupon_code
        self.code = SecureRandom.hex(32) unless self.code
    end
end
优惠券金额=100
优惠券的数量。次do
offer.coups.create({:attr=>value})
结束
类优惠券
很难说你在这方面寻求帮助。您不确定如何生成唯一优惠券代码,不确定如何对其建模,不确定如何一次创建多个记录吗?我会试着给出一个大概的答案,如果我在建这个,我会怎么做,也许这会对你有所帮助

我将创建两个模型,
提供
优惠券

rails g model offer title:string user:references
rails g model coupon offer:references code:uuid
我不知道你使用的是mysql还是postgresql。我认为uuid只适用于postgresql。如果您使用的是mysql,我想应该将代码列改为字符串。或者,最好切换到postgresql

通常,当您创建优惠券时,您只需执行以下操作:

offer.coupons.create!
在创建之前,它会在
钩子中为您生成代码。但是,由于您希望一次创建100个这样的对象,简单的方法是:

100.times { offer.coupons.create! }
但这会有点低效。我不知道如何让Rails高效地插入许多记录,因此我们将手动执行:

objs = []
100.times do
  objs.push "(#{offer.id}, '#{SecureRandom.uuid}', '#{Time.now}', '#{Time.now}')"
end

Coupon.connection.execute("INSERT INTO coupons (offer_id, code, created_at, updated_at) VALUES #{objs.join(", ")}")

如果有更讽刺的方法,请有人告诉我们。

优惠券的数量由创建优惠的用户选择。@figdig优惠券的数量只是一个参数,您可以从参数中分配它。优惠券的数量由创建优惠的用户选择。您试过什么吗?
100.times { offer.coupons.create! }
objs = []
100.times do
  objs.push "(#{offer.id}, '#{SecureRandom.uuid}', '#{Time.now}', '#{Time.now}')"
end

Coupon.connection.execute("INSERT INTO coupons (offer_id, code, created_at, updated_at) VALUES #{objs.join(", ")}")