我有一个积极的记录模式,它包含一个序列化领域,其中包括一个习惯式的非活跃记录模式(一个简单的类别,加上多个属性)。
我试图拿出一种办法,将每一序列化项目与母体模式结合起来,但无法找到这样做的有利途径。
The serialized model "initialize" method receives an "attributes" hash, and simply populates the class attributes with those. This is just fine, beside the fact that when receiving the data from a form, the data is received as a string data rather than an actual type. ActiveRecord model knows how to deal with this, and does typecasting based on the Schema before the model instance is saved to the db. Is there anything I can do (beside typecasting right in the "initialize" method) to make the behavior similar to the AR models?
此外,我试图利用“Validatable”gem验证我的NON AR-Model,但似乎像现在一样,在使用“有效日期-数字-of”和确定该数字大于0时时时,无法加以界定。
可向其传递“如果”参数,但在数字核对之前,然后执行时:
validates_numericality_of :price, :if => lambda { self.price >= 0.0 }, :message => "Price must be greater or equal to 0"
如果我进入“价格”领域,在我试图检查该模式是否有效时,我就获得一个例外,因为“价格”是一种扼杀,试图将其与编号......进行比较。
我希望,我会问两个问题,一看一看......但又是相关的。
成就