I have a Sponsor model and a promo code model.
- Sponsor may have zero or more promotional codes.
- An ad code can have sponsors zero or one .
Thus, the promotional code must have an optional link to the sponsor, that is sponsor_id, which may or may not matter. I am not sure how to install this in Rails.
Here is what I still have:
class Sponsor < ActiveRecord::Base
has_many :promo_codes
end
class PromoCode < ActiveRecord::Base
has_one :sponsor
end
class AddSponsorReferenceToPromoCodes < ActiveRecord::Migration
def change
add_reference :promo_codes, :sponsor, index: true
end
end
It makes sense? I got the impression that I should use belongs_topromotional codes in my model, but I have no reason for this, I just have not seen an example has_manywith has_one.