2

我对红宝石比较陌生。这个可以晒干吗?如何?

def primary_phone
  number_to_phone self.primary_phone_number
end

def primary_phone=(number)
  self.primary_phone_number = number.gsub(/\D/, '')
end

def secondary_phone
  number_to_phone self.secondary_phone_number
end

def secondary_phone=(number)
  self.secondary_phone_number = number.gsub(/\D/, '')
end

def fax
  number_to_phone self.fax_phone_number
end

def fax=(number)
  self.fax_phone_number = number.gsub(/\D/, '')
end

我在 Rails 中使用此代码作为数据库之间的一种缓冲区,数据库将数字存储为整数,用户表单将它们作为字符串输入。

4

1 回答 1

7

所有你需要的是,

before_save :sanitize_phone_number_fields

def sanitize_phone_number_fields
  [:primary_phone, :secondary_phone, :fax].each{|attr| self.send(attr).gsub!(/\D/, '')}
end
于 2013-08-29T18:39:26.647 回答