ruby on rails 3 - Sub domain validation and creation from backend -
hi have application in user registers , provide company name , utilize name , create sub domain saved in company table. want create unique don't want restrict user type same company name in database. want if company name nowadays example: name entered xyz sub domain xyz.myapp.com, , when other user type same name sub domain should xyz1.myapp.com , same other same names in sequence.
here have tried fails on sec turn:
def get_available_subdomain generated_subdomain = name.downcase.gsub(/\s+|\&|\@|\#|\(|\)|\/|\.|\/|\?|\!|\"|\$|\%|\'|\*|\+|\,|\:|\;|\<|\>|\[|\]|\^|\`|\{|\}|\||\-|\~/, "") unless name.blank? companies = company.where(:subdomain=> generated_subdomain) if companies.count == 0 new_subdomain = generated_subdomain else new_subdomain = generated_subdomain + (companies.count).to_s end new_subdomain end
how create unique method expected results. help appreciated.
if using mysql back-end can seek searching companies generated sud-domain 'xyz', 'xyz1', 'xyz2' etc.
def get_available_subdomain generated_subdomain = name.downcase.gsub(/\s+|\&|\@|\#|\(|\)|\/|\.|\/|\?|\!|\"|\$|\%|\'|\*|\+|\,|\:|\;|\<|\>|\[|\]|\^|\`|\{|\}|\||\-|\~/, "") unless name.blank? companies = company.where('subdomain regexp ?', "#{generated_subdomain}[/d]*") if companies.count == 0 new_subdomain = generated_subdomain else new_subdomain = generated_subdomain + (companies.count).to_s end new_subdomain end
if database progress search companies this
companies = company.where('subdomain ~* ?', "#{generated_subdomain}[/d]*")
if database not mysql suggested search company name instead of sub-domain. because creating subdomain according company name.
companies = company.where(:name => generated_subdomain)
ruby-on-rails-3 subdomain
No comments:
Post a Comment