I have to say that after being a Rails user for some time I have grown
to really dislike how ActiveRecord (AR) classes - models - are being
pushed as the defacto method for getting form data into the DB. The
two concepts are completely separate and should be handled by
completely different classes.
Anyone who understands Object Oriented Design knows that we separate
classes based on functionality and responsibility. An automobile
class, for example, would represent something that has wheels and
drives; a fixed-wing aircraft class would represent something that
flies. Such is the case with AR, it handles the interaction with the
database and holds the data, and perhaps has some extra methods that
interact with the data it holds. Also, what we see a lot of is having
the AR class also handle data coming from a form, which breaks
encapsulation. It’s like saying that to build the ultimate vehicle
would be to put wings on a car because it would be get us in a jiffy
whether we had to go short or long distances (a nice idea, but try
driving down Broadway with wings spanning forty feet).
The problem with coupling the form and the AR is that it may have very
little resemblance to the form that is creating it. I’ve encountered
this quite a bit. For registration (user model) I had to add a number
of attributes to the class that had no mapping to the db, that hung
around and cluttered up the design tremendously. Furthermore, the
need to have those attributes validated compounded the problem further
since some could only be validated on create or on update. Also
whenever I wanted to save the object when not in form mode i always
needed extra code to flag or message the object so that it wouldn’t
validate itself - a time consuming and unnecessary task.
The ultimate solution was to do what OOD says to do - break out the
forms into a completely separate class that handled and validated the
form data and then passed the data to the AR object. Needless to say,
it took quite a bit of time to do, and I think it’s the best
solution. I hope that the next versions of Rails will contain some
kind of ActiveForm class for the sake of others.