4

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.

Now... I cannot validate one by one model and to enter 4 of them in database. 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

And in registration process I need to include username, name, account_number and to validate them. How to do that?

Haris Bašić
  • 1,383
  • 2
  • 12
  • 21
  • have a look at [nested forms](http://railscasts.com/episodes/196-nested-model-form-part-1) and some [existing questions](http://stackoverflow.com/questions/19273698/how-to-make-sure-child-object-is-valid-while-saving-parent). the idea is that you can deny saving a parent object if child objects are invalid. – twonegatives Jun 13 '14 at 08:39
  • Also have a look at delegation: http://apidock.com/rails/Module/delegate – ReggieB Jun 13 '14 at 08:47

3 Answers3

2

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.

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


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.

This means that every model you load is just a class which has been populated with a series of getter / setter methods. These methods are defined by ActiveRecord from your data table columns, and are why you can call @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

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
1

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

LA Ruby Conference 2013 Refactoring Fat Models

ActiveModel Form Objects

dre-hh
  • 7,840
  • 2
  • 33
  • 44
1

I understand that you multistep registration. You shouldn't create 4 models only because your view pages needs it. You should:

  1. remove validation from User model and add validation on each form
  2. create 4 different forms (for example extends by ActiveModel or user gem reform)
  3. add validation to each form
  4. after form.valid? save part of user info to @user object

Thats all.

uselesssmile
  • 124
  • 9