0

I have a basic app that allows user1 to create a redeemable code. user1 shares this code with his friends and user2,3,4 redeem the code. At each redemption I need to to increment user1's points attribute.

the first time a user redeems the code, it creates a redemption. this never works again as the user can only redeem a code once. another user can redeem it, but not more than once each.

So I figured I would use a before or after_create callback. Here is my redemption class:

class Redemption < ActiveRecord::Base
  has_one :code
  belongs_to :user

  validates :code_id, :presence => true
  validates :user_id, :presence => true

  before_create :increment_points

  def increment_points
    self.user.increment(:points)
  end 

end

I tried this, and it did not return any errors, it handled the redemption but did not increment points...

ideas?

TJ Sherrill
  • 2,465
  • 7
  • 50
  • 88

2 Answers2

4

What Benjamin said above. Increment does not save the model. Increment! does.

http://apidock.com/rails/ActiveRecord/Persistence/increment%21

redronin
  • 713
  • 4
  • 9
0

I think the issue lies with you not saving the user model. Something like self.user.save! might do the trick.

Benjamin Tan Wei Hao
  • 9,621
  • 3
  • 30
  • 56