Skip to content

Instantly share code, notes, and snippets.

@marocchino
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marocchino/8817691 to your computer and use it in GitHub Desktop.
Save marocchino/8817691 to your computer and use it in GitHub Desktop.
class A < ActiveRecord::Base
validates :phoneNum1, numericality: { only_integer: true }
validates :phoneNum2, numericality: { only_integer: true }
validates :phoneNum3, numericality: { only_integer: true }
def phoneNum1
phoneNum && phoneNum.split("-")[0]
end
def phoneNum2
phoneNum && phoneNum.split("-")[1]
end
def phoneNum3
phoneNum && phoneNum.split("-")[2]
end
def phoneNum1=(phoneNum1)
@phoneNum1 = phoneNum1
set_phoneNum
end
def phoneNum2=(phoneNum3)
@phoneNum2 = phoneNum3
set_phoneNum
end
def phoneNum3=(phoneNum3)
@phoneNum3 = phoneNum3
set_phoneNum
end
private
def set_phoneNum
if @phoneNum1 && @phoneNum2 && @phoneNum3
self.phoneNum = "#{@phoneNum1}-#{@phoneNum2}-#{@phoneNum3}"
end
end
end
class A < ActiveRecord::Base
validates :phoneNum, ...
end
class AController < ApplicationController
def create
A.create(a_params)
end
private
def a_params
params[:a][:phoneNum] = "#{params[:a][:phoneNum1]}-#{params[:a][:phoneNum2]}-#{params[:a][:phoneNum3]}"
params[:a].permit(...)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment