User
has_many :codes
has_many :redemptions
Code
belongs_to :user
has_many :redemptions
Redemptions
has_one :code
belongs_to :user
Redemptions Model:
class Redemption < ActiveRecord::Base
has_one :code
belongs_to :user
validates :code_id, :presence => true
validates :user_id, :presence => true
after_create :increment_points
def increment_points
self.code.user.increment!(:points)
end
end
When I create a new redemption it returns the following error:
PGError: ERROR: column codes.redemption_id does not exist
LINE 1: SELECT "codes".* FROM "codes" WHERE "codes"."redemption_id...
^
: SELECT "codes".* FROM "codes" WHERE "codes"."redemption_id" = 17 LIMIT 1
Is something wrong with my associations? I have mapped it out and believed I could back track up the association redemption -> Code -> User
The one caveat to this issue is that a redemptions user is not the user that I am trying to increment points on. The code was created by a user and that is the user I am trying to update…
thoughts?
has_many/one and belongs_to needs to go together. when you declare Redemption has_one :code, Rails expects you put a redemption_id on codes table and have the corresponding belongs_to declared on Code.