Ruby on Rails: How to validate a model without Active Record?

前端 未结 2 607
情话喂你
情话喂你 2021-01-03 21:09

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

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

2条回答
  •  清酒与你
    2021-01-03 21:43

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

    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.

    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.

提交回复
热议问题