Ruby on Rails的:如何验证没有Active录制模式? [英] Ruby on Rails: How to validate a model without Active Record?

查看:132
本文介绍了Ruby on Rails的:如何验证没有Active录制模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图验证域,而无需一个的ActiveRecord :: Base的继承。

I'm currently trying to validate fields without having an ActiveRecord::Base inheritance.

我的模型存储的缓存服务器上的数据,所以我不需要ActiveRecord的。

My model stores the data on a cache server so I do not need ActiveRecord.

不过,我想验证模型的领域,如我想如果我使用的ActiveRecord(如 validates_numericality_of:数量:greater_than => 0 )?

Anyway, I would like to validate the fields of the model like I would if I was using ActiveRecord (e.g validates_numericality_of :quantity, :greater_than => 0) ?

我怎么能这样做?

非常感谢你的帮助。

推荐答案

在Rails 3,活动模型包含活动记录的非数据库的功能。

In Rails 3, Active Model contains the non-database functionality of Active Record.

基本上,你需要包括加载ActiveModel ::验证,定义字段 attr_accessor ​​,使用初始化方法来初始化属性,使他们非持久化模型不保存到数据库中。

Basically, you need to include ActiveModel::Validations, define your fields as attr_accessor, use an initialize method to initialize the attributes and make them non-persisted as your model isn’t persisted to a database.

这样你就可以对无表格模型验证和控制器一样的,如果你使用活动记录。还有一个Railscast这个<一href="http://railscasts.com/episodes/219-active-model">http://railscasts.com/episodes/219-active-model.

This way you can have validations on the tableless model and your controller the same as if you were using Active Record. There's also a Railscast on this http://railscasts.com/episodes/219-active-model.

这篇关于Ruby on Rails的:如何验证没有Active录制模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆