매우 그렇다, 당신이 할 필요가 self.dimensions의 값을 설정하는 콜백을 사용하는 것입니다 : 당신은 당신이 어떤을 유지하도록 self.foo || self.dimensions[:foo]
를 사용할 필요가
class ShippingProfile < ActiveRecord::Base
after_initialize :set_default_dimensions
after_validation :set_dimensions
serialize :dimensions, Hash
attr_accessor :width, :height, :depth
attr_accessible :width, :height, :depth, :dimensions
private
def set_default_dimensions
self.dimensions ||= {:width => 0, :height => 0, :depth => 0}
end
def set_dimensions
self.dimensions = {
:width => self.width || self.dimensions[:width],
:height => self.height || self.dimensions[:height],
:depth => self.depth || self.dimensions[:depth],
}
end
end
을 이미 해시에 설정된 기존 값 왜? 귀하의 차원 특성 (전 가정) 데이터베이스에서 유지되지 않습니다 - 당신 attr_accessor, 테이블에 필드로 그들을 설정하는 대신 사용하고 있습니다.
제쳐두고, 나는 모델 디자인에 대해 잘못된 생각을하고 있다고 생각합니다. 데이터베이스에 해시로 차원을 저장하면 이러한 특성을 기반으로 쿼리 할 수있을뿐만 아니라 불필요한 취약성을 추가 할 수 있습니다.
이 개별 특성을 개별 필드로 저장하는 인 경우 중복성과 복잡성을 도입하게됩니다.
class ShippingProfile < ActiveRecord::Base
def dimensions
{ :width => self.width, :height => self.height, :depth => self.depth }
end
end
이런 식으로, 당신은 기능을 유지 : 당신은 더 나은이 필요한 경우 다음 즉석에서 치수 해시를 생성, (당신이 이미하지 않은 경우) 데이터베이스의 필드로 세 가지 특성을함으로써 제공 될 것입니다 유연성을 확보 할 수 있습니다.
당신은 직렬화의 클래스를 사용할 수 있습니다
ShippingProfile.find (1) .dimensions.width = 4가 더 좋을 것이라고 생각하지 않습니까? – macarthy