Re: Of Models and Controllers

Chris Lambert
Thu, 18 Aug 2005 00:05:32 -0000

To call Model methods from the controller, it''s identical to how you
do so in a view.

def play_move
  @player = Player.find_by_name(params[:name])
  @player.score_region
end

Then, @player is exposed to the template, but all the player-specific
scoring details are handled in your Player model''s score_region
method.

Is this what you''re asking?

-- Chris

On 8/17/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> 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/
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >