tag:blogger.com,1999:blog-6793817269158736634.post4889596562267939454..comments2023-07-11T06:44:05.157-07:00Comments on Code It Like You Stole It: Adding two columns in a table with RailsTylerhttp://www.blogger.com/profile/02372203985854517525noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-6793817269158736634.post-14487936869346903472011-08-12T06:49:41.085-07:002011-08-12T06:49:41.085-07:00Yeah, the complexity of the method you showed me i...Yeah, the complexity of the method you showed me is part of the reason I went looking for a simpler method. I already knew there's more than one way of doing things with Ruby and here we are!Tylerhttps://www.blogger.com/profile/02372203985854517525noreply@blogger.comtag:blogger.com,1999:blog-6793817269158736634.post-48912820502582713692011-08-12T06:34:58.256-07:002011-08-12T06:34:58.256-07:00This one is specific, explicit, and obvious. The o...This one is specific, explicit, and obvious. The other one is general, implicit, and much more complex, but it should work on ALL models that behave like these do.<br /><br />It's programming, you have to choose your own poison :)Elijah Millerhttps://www.blogger.com/profile/11416221333112662701noreply@blogger.comtag:blogger.com,1999:blog-6793817269158736634.post-58211876534381609972011-08-11T19:54:14.822-07:002011-08-11T19:54:14.822-07:00Aha, I knew it'd be something simple like that...Aha, I knew it'd be something simple like that. Awesome!<br /><br />I figured out count wasn't what I wanted, after I finally got it to work. Any potential shortcomings to using this method as opposed to the one you showed me a while back? I'll be using this total method for several different models, and each will work similar to this, though they'll have more integers to be added.Tylerhttps://www.blogger.com/profile/02372203985854517525noreply@blogger.comtag:blogger.com,1999:blog-6793817269158736634.post-60143627151497108542011-08-11T19:33:02.901-07:002011-08-11T19:33:02.901-07:00Try this:
class Initiative < ActiveRecord::Base...Try this:<br />class Initiative < ActiveRecord::Base<br /> # ...<br /> def total<br /> dex + misc<br /> end<br />end<br /><br />@initiative.total # => dex + misc<br /><br />to_s is automatically called by <%= %>.Elijah Millerhttps://www.blogger.com/profile/11416221333112662701noreply@blogger.com