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?
What Benjamin said above. Increment does not save the model. Increment! does.
http://apidock.com/rails/ActiveRecord/Persistence/increment%21