Re: Of Models and Controllers

Pat Maddox
Wed, 17 Aug 2005 22:45:16 -0000

Put validations in your model, so that errors come up whenever save is
called.  Those validations would be to make sure that all the data in
the db is good...points can''t be negative, player name must be greater
than X chars, etc.

Then in your controller, do some higher-level checks.  For example, if
when you call give_points, the number of points must be positive, you
can perform the check there.

In general, I do model validation for all of the actual model data.
Any user input requiring constraints that modifies the data gets
checked in the controller.  So as above, the model''s points would be
validated, but the points added would be checked in the controller.

Pat



On 8/17/05, Johan Svensson
<echo5ive-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> The IRC channel is kinda busy, so I''ll ask here instead in peace
and quiet.
>
> My first Rails application, a system for managing raids in World of
> Warcraft, is getting feature-complete. All that''s left is the
logic to
> assign points to participating players.
>
> I''m not quite sure where this kind of logic goes, though. The
> controller seems like the most logical place, but so far all I''ve
done
> in the controller is juggle data from the models to the views.
>
> I would also like to have the actual logic for changing the player
> points in the Player model instead of in the controller so I can just
> call the approriate scoring method from the model, but I''m not
sure
> how to call it from the controller.
>
> The method in the Player model is very simple until I can get it working:
>
>         def give_points(target, pts)
>                 target.points += pts
>                 # No failsafe checks here yet
>                 target.save
>         end
>
> If I can get some pointers on how to call the methods, I can probably
> figure out the rest on my own.
>
> --
> Johan Svensson
> http://atomicplayboy.net/