1
그래서 hstore는 키와 특히 값 중 하나를 문자열에만 저장한다는 것을 알고 있습니다. 나는 그들의 데이터 유형을 설정하는 방법을 찾고있다. 레일이나 hstore에이 작업을 수행 할 수있는 방법이 있습니까?Rails 4 hstore 데이터 유형 정의
지금까지 내가 한 것은 getter를 재정의하고 내가 원하는 데이터 유형에 따라 다릅니다. 이것은 내가 지금까지 가지고있는 것입니다 :
class ModelWithHstore < ActiveRecord::Base
store_accessor :properties, :some_boolean_field, :some_integer_field, :some_datetime_field
validate :validate_range
def some_boolean_field
return if self[:properties].nil? || self[:properties][__method__.to_s].nil?
if [true, 'true', '1'].include? self[:properties][__method__.to_s]
return true
elsif [false, 'false', '0'].include? self[:properties][__method__.to_s]
return false
end
self[:properties][__method__.to_s]
end
def some_integer_field
return if self[:properties].nil? || self[:properties][__method__.to_s].nil?
self[:properties][__method__.to_s].to_i
end
def some_datetime_field
return if self[:properties].nil? || self[:properties][__method__.to_s].nil?
DateTime.strptime(self[:properties][__method__.to_s].to_s, '%F %T')
end
private
def validate_range
errors.add(:some_integer_field, "value out of range") if !some_integer_field.between?(10, 90)
end
end
그리고 그들은 getters이기 때문에입니다. 나는 그들이 유효성 검사기 및 다른 장소에서도 사용되고 있다고 생각합니다. 그러나 이와 같은 것이 이미 존재하는지 또는 이것을 구현하는 더 좋은 방법이 있는지 정말로 모르겠습니다.
건배!