I'm building an application where users can purchase tracking numbers. I have an Order model and an Order Transaction model. If the Order Transaction returns from the gateway with success, I'm using an after_save callback to trigger a method that creates the tracking numbers and inserts them into the database. Sometimes a user just orders one, but if they order more than one, I can't seem to get rails to create and insert more than one record.
Here's what I'm using -- I've never had to user a loop like this, so I'm not sure what I'm doing wrong.
def create_trackables
if self.success == true
@order = Order.find(order_id)
@start = 0
while @start < @order.total_tokens
@trackable_token = Tracker.create_trackable_token
@start += 1
@trackable ||= Tracker.new(
:user_id => @current_user,
:token => @trackable_token,
:order_id => order_id
)
@trackable.save
end
end
end