On the other hand, thanks to help from @jqr and a healthy does of trial and error, I've gotten that pesky statistic bonus problem figured out. This is what ended up working:
def total
fortitude_base + character.statistic.con_modifier + magic + misc
end
def con_modifier
(constitution.to_i - 10) / 2
end
I'm keeping it as .to_i now, as a fix for a case where a player tries to create a Fortitude save before a Constitution score has been entered. The .to_i translates a nil record to 0, so Rails does a temporary calculation (which isn't shown) rather than displaying an error. Not sure if there's a better way to handle this (perhaps simply checking that statistic.constitution.present? before performing any calculations) but it works for now.
More importantly than simply figuring this one instance of how to define logic in one model and then call it in the view of another is understanding HOW it works. I can extrapolate these methods and apply them to other things: skills, attack rolls, armor class, etc, etc. Once again, Manticore proves to be a useful learning tool! SUCCESS!
The main thing to think about when dealing with the different variable types is scope.
ReplyDeleteLocal variables have limited scope and exist within the method, block, etc. they're defined within. Starting out, just think of them as variables that aren't associated with an object's state. They're the ones you'd use within methods to temporarily hold values while performing calculations or procedures.
Instance variables are useful for maintaining object state. If you're holding a value within an instance for use by multiple methods or across multiple accesses, use one of these.
You're probably dealing with these primarily in the context of controllers and views. When you make a request, it's routed to the appropriate controller. An instance of this controller is initialized and the appropriate action called. Within this action, you set instance variables which, through some Rails magic, are accessible within the view. Try not to let this Rails behavior confuse you as to the basic nature of the variable types.
Straightforward documentation:
http://www.rubyist.net/~slagell/ruby/localvars.html
http://www.rubyist.net/~slagell/ruby/instancevars.html