简体   繁体   English

先进的ruby on rails模型和验证

[英]Advanced ruby on rails models and validation

In general, I have a website which needs to have complex registration process. 一般来说,我有一个网站,需要有复杂的注册过程。 And in that registration process I need to include 4 tables from database. 在该注册过程中,我需要从数据库中包含4个表。

Now... I cannot validate one by one model and to enter 4 of them in database. 现在......我无法逐个验证模型并在数据库中输入其中的4个。 Is there a way to make common points of all those tables in one model? 有没有办法在一个模型中制作所有这些表的共同点?

Let's say: User model has columns: username, name , etc. Package model has: type, account_number etc 假设: User模型有列: username, name等。 Package模型有: type, account_number

And in registration process I need to include username, name, account_number and to validate them. 在注册过程中,我需要包含用户名,名称,account_number并验证它们。 How to do that? 怎么做?

Without seeing your model structure, this is just speculation, but here goes: 如果没有看到你的模型结构,这只是猜测,但这里有:

-- -

Virtual Attributes 虚拟属性

In your User model, you can use attr_accessor to create a set of virtual attributes - which basically mean you can create a series of setter / getter methods in your User model. User模型中,您可以使用attr_accessor创建一组virtual attributes - 这基本上意味着您可以在User模型中创建一系列setter / getter方法。

Although I don't think this will help you directly, it should give you an idea as to how you can create single-model validation: 虽然我认为这不会直接帮助您,但它应该让您了解如何创建单模型验证:

#app/models/user.rb
Class User < ActiveRecord::Base
    attr_accessor :new, :values, :to, :validate
    validates, :new, :values, :to, :validate, presence: true
end

This will allow you to create the attributes in the User model - and although they won't be saved, you can then use them to validate against 这将允许您在User模型中创建属性 - 虽然它们不会被保存,但您可以使用它们进行验证


attr_accessor

To give you a quick overview of this method, you first need to remember that Rails is just a collection of modules and classes . 为了快速概述这个方法,首先需要记住Rails只是modulesclasses的集合。

This means that every model you load is just a class which has been populated with a series of getter / setter methods. 这意味着,每一个model加载仅仅是一个class已经填充了一系列getter / setter的方法。 These methods are defined by ActiveRecord from your data table columns, and are why you can call @user.attribute 这些方法由ActiveRecord在数据表列中定义,这也是您可以调用@user.attribute

The magic is that if you use attr_accessor , you'll basically create your own attributes in your User model - which won't be saved in the database, but will be treated like the other attributes your objects have, allowing you to validate them 神奇的是,如果你使用attr_accessor ,你基本上会在你的User模型中创建自己的属性 - 这些属性不会保存在数据库中,但会被视为对象拥有的其他属性,允许你验证它们

Because your registration process seems to be complex, I would go even futher as virtual attributes and use Form Objects 因为您的注册过程看起来很复杂,所以我会更进一步作为虚拟属性并使用表单对象

7 Patterns to Refactor Fat ActiveRecord Models 重构FatRecord模型的7种模式

LA Ruby Conference 2013 Refactoring Fat Models LA Ruby Conference 2013重构脂肪模型

ActiveModel Form Objects ActiveModel表单对象

I understand that you multistep registration. 我了解你多步注册。 You shouldn't create 4 models only because your view pages needs it. 您不应仅因为视图页面需要而创建4个模型。 You should: 你应该:

  1. remove validation from User model and add validation on each form User模型中删除验证并在每个表单上添加验证
  2. create 4 different forms (for example extends by ActiveModel or user gem reform ) 创建4种不同的形式(例如通过ActiveModel扩展或用户gem reform
  3. add validation to each form 为每个表单添加验证
  4. after form.valid? form.valid?之后form.valid? save part of user info to @user object 将部分用户信息保存到@user对象

Thats all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM