Reputation: 2645
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?
Upvotes: 0
Views: 1777
Reputation: 723
What Benjamin said above. Increment does not save the model. Increment! does.
http://apidock.com/rails/ActiveRecord/Persistence/increment%21
Upvotes: 4
Reputation: 9691
I think the issue lies with you not saving the user model. Something like self.user.save!
might do the trick.
Upvotes: 0