Chris Burrus
Chris Burrus

Reputation: 187

Creating default resources for rails

My overarching model is User, which have placements.

placements has_and_belongs_to_many campaigns, and visa-versa obviously.

Campaigns are a simple model that just has a campaign name.

class Campaign < ActiveRecord::Base
  belongs to :user
  has_and_belongs_to_many :placements

  validates_presence_of :campaign_name
end

My question is - currently I am displaying a dropdown menu with all of the Campaigns assigned to the the placement. What I want to do though, is create a 'default' campaign of sorts, called "No Campaign Specified". So if the user wants to create a new campaign, it will appear in the dropdown menu, or they can just use the default "No Campaign Specified" if they don't want to.

I have most of this done, except I can't figure out - how do I have a default database entry for the resource :campaign ? Just setting the default in the migration doesn't work, as that just sets the default text field. I want the user to never have to create a campaign if they don't want to, they can just use a default one the program assigns to every placement. What would be the best way to implement this?

Upvotes: 0

Views: 32

Answers (1)

alf
alf

Reputation: 18540

Actually, having a Campaign instance to indicate that the user doesn't have a Campaign doesn't sound like a good idea. If the only reason you're doing that is to show "No Campaign Specified" on a dropdown, then you should use the :prompt property of the select tag. Please take a look at this answer for more information: https://stackoverflow.com/a/9696834/512507

Upvotes: 1

Related Questions