Re: Beginner: helper vs model

Scott Peterson
2008-11-28T19:35:45Z

Rob Biedenharn wrote:
> On Nov 28, 2008, at 1:51 PM, Scott Peterson wrote:
>> So, I''ve generated a player_admin controller and pointed it to
my
>> object using debug(), which didn''t show fullname as an
attribute.
>> end
> You really want to have this in your Player model.  It will look
> almost the same:
>
> class Player
>    def fullname
>      self.first_name + " " + self.last_name
>    end
> end
>
> or perhaps:
>
> class Player
>    def fullname
>      "#{self.first_name} #{self.last_name}"
>    end
> end
>

Absolutely right.  I want a single place which defines how I build
"fullname", so when I add title, middle name, whatever, it just works
everywhere I display.

>> funadmentally
>> wrong with how I''m thinking about Ruby objects, that in the
Player
>> model
>> wasn''t really an appropriate place to try that?
>>
>> Thank you.
>
> How did the method look when you tried to define it within your Player
> model?  If you still tried to refer to @player rather than self, that
> may be your mental hurdle.  You''re defining a new attribute of a
> model.  The rest of the system shouldn''t care if the storage
behind
> the model changes to hold the fullname and the other methods become:
>
> class Player
>    def first_name
>      self.fullname.split('' '').first
>    end
>    def last_name
>      self.fullname.split('' '').last
>    end
> end
>
> (NOTE: I''m not suggesting that this should happen, but only that
the
> rest of the application can continue to reference @player.first_name,
> @player.last_name, or @player.fullname without having to know what
> really sits behind the model.)
>
> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org


Thanks a million for the response.  Not sure what I did yesterday.  I''m
pretty sure I wasn''t referring to "@player", as I was in the
Player
model object.  Some foolish typo, I imagine.  Glad to hear that I had
the right idea though.
--
Posted via http://www.ruby-forum.com/.