I’m trying to implement the following:
- A
Pagehas different ad spots. E.g.leftandright. - There is a
Campaignmodel representing ad campaigns. - Each ad spot can be filled with one
Campaignat the time. E.g.Page 1 lefthasCampaign 1attached.Page 1 righthas alsoCampaign 1attached.
I could just add left_campaign_id and right_campaign_id to the pages table and use belongs_to twice in the model. But since the amount of ad spots may change over time I feel like that’s not flexible enough.
So I was thinking about using a CampaignPlacement model with has_many :through. The migration would look like this:
class CreateCampaignPlacement < ActiveRecord::Migration[5.0]
def change
create_table :campaign_placements, id: :uuid do |t|
t.uuid :campaign_id, null: false
t.uuid :page_id, null: false
t.integer :ad_spot
t.timestamps
end
end
end
The issue: I want to enforce only one campaign per Page and spot. And the user should select the campaign per spot through a select tag. This is where I’m starting to doubt if has_many :through is the correct approach. I looked at has_one :through as well, but doesn’t really seem to fit either.
Am I overlooking something? As things stand I’d probably keep using has_many :through and implement the logic to have only one relationship myself at the controller level. Feels wrong though.