Re: Of Models and Controllers

Ezra Zygmuntowicz
Wed, 17 Aug 2005 23:53:10 -0000

On Aug 17, 2005, at 3:32 PM, Johan Svensson 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
>
  If the give_points method is in your Player model and you want to
call it form your controller then you need to put a self in there
like this:

>     def self.give_points(target, pts)
>         target.points += pts
>         # No failsafe checks here yet
>         target.save
>     end

Then in your controller you can call the method with
Player.give_points(...). Or if you have a player object already
assigned to a var :@player  then you can say @player.give_points
(...). I think that is what you are asking for. Forgive me if I
misinterpreted your question.
HTH
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
509-577-7732