English 中文(简体)
铁路司2.3 编外和验证非AR型号
原标题:Ruby on Rails 2.3 populating and validating non-AR models from form params

我有一个积极的记录模式,它包含一个序列化领域,其中包括一个习惯式的非活跃记录模式(一个简单的类别,加上多个属性)。

我试图拿出一种办法,将每一序列化项目与母体模式结合起来,但无法找到这样做的有利途径。

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"

如果我进入“价格”领域,在我试图检查该模式是否有效时,我就获得一个例外,因为“价格”是一种扼杀,试图将其与编号......进行比较。

我希望,我会问两个问题,一看一看......但又是相关的。

成就

问题回答

您是否尝试了<条码>。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签