validate uniqueness, don't allow duplicate campaigns.

This commit is contained in:
Tyrel Souza 2014-04-08 17:01:22 -04:00
parent dd0c0e4ab0
commit ab906fdd00
3 changed files with 7 additions and 3 deletions

View File

@ -22,7 +22,9 @@ class CampaignsController < ApplicationController
# POST /campaigns # POST /campaigns
def create def create
@campaign = Campaign.new(campaign_params) clientid = campaign_params.delete(:clientid)
@campaign = Campaign.find_or_create_by(clientid: clientid)
@campaign.update(campaign_params)
if @campaign.save if @campaign.save
respond_to do |format| respond_to do |format|
@ -57,11 +59,11 @@ class CampaignsController < ApplicationController
# Only allow a trusted parameter "white list" through. # Only allow a trusted parameter "white list" through.
def campaign_params def campaign_params
params.require(:campaign).permit(:clientid, :listingcode, :billingcode, :advertisername, :address1, params.require(:campaign).permit(:id, :clientid, :listingcode, :billingcode, :advertisername, :address1,
:address2, :city, :state, :zip, :emailaddress, :customerfirstname, :address2, :city, :state, :zip, :emailaddress, :customerfirstname,
:customerlastname, :salesrep, :salesrepemail, :businessphone, :customerlastname, :salesrep, :salesrepemail, :businessphone,
:contactphone, :websiteurl, :contactphone, :websiteurl,
videolistings_attributes: [:videocode, :producttypeid, :remoteassetsarchive, :voiceoverselection, videolistings_attributes: [:id, :videocode, :producttypeid, :remoteassetsarchive, :voiceoverselection,
:musicselection, :clientprovidedscript, :keywords, :focus, :notes, :asseturls,]) :musicselection, :clientprovidedscript, :keywords, :focus, :notes, :asseturls,])
end end

View File

@ -1,4 +1,5 @@
class Campaign < ActiveRecord::Base class Campaign < ActiveRecord::Base
has_many :videolistings has_many :videolistings
accepts_nested_attributes_for :videolistings accepts_nested_attributes_for :videolistings
validates :clientid, presence: true, uniqueness: true
end end

View File

@ -1,3 +1,4 @@
class Videolisting < ActiveRecord::Base class Videolisting < ActiveRecord::Base
belongs_to :campaign belongs_to :campaign
validates :videocode, presence: true, uniqueness: true
end end