J
J
jj_killer2013-01-29 00:05:48
MongoDB
jj_killer, 2013-01-29 00:05:48

RoR. Model with many attributes and their validation

I'm talking about railroaders. Suppose we have a model:

class Event
  include Mongoid::Document

  field :action, type: Symbol
  field :actor_structure, type: Array
  # и так еще штук 40 полей, то символов, то массивов

end


Each field must correspond to a value from the list of possible ones. Each field has its own list. New values ​​are frequently added to the list. The first thing that comes to mind is has_many and has_and_belongs_to_many. But I'm afraid that this will greatly slow down the selection of the model and in general, perhaps even the anti-pattern.

You can leave them as attributes by creating models corresponding to them, and write a custom validator for each attribute. Although this is also, most likely, an anti-pattern.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EugeneOZ, 2013-01-29
@EugeneOZ

I'm not a railsman, but I think your second option with validators is normal.

S
sl_bug, 2013-01-29
@sl_bug

In the case of adding a new value, we correct the validators, sadness ...
has_many is an option. if you need to pull with dependencies, that is, includes
But in any case, real data will be useful to help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question