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/
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 >
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 > endThen 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
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 > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Thu, 2005-08-18 at 00:32 +0200, Johan Svensson wrote:> 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 > endI think it seems odd to want to pass the target in when you can simply do something like def give_points(pts) # maybe a check for error input on pts. self.points += pts self.save end And in your controller you would do something like target = Player.find(tgt) target.give_points(pts) -- Steven Critchfield critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org KI4KTY