# Beginner: helper vs model

**URL:** <https://www.ruby-forum.com/t/beginner-helper-vs-model/154403>\
**Category:** Rails\
**Created:** [November 28, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/beginner-helper-vs-model/154403 "2008-11-28T19:51:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 28, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/beginner-helper-vs-model/154403/1 "2008-11-28T19:51:45Z")

</div>

Just learning the framework. I apologize if this is in the worng forum.  
I looked around for something like “Beginner’s Questions” but didn’t see  
it.

I’m building a simple application which will let players register for an  
event.

So, I’ve generated a player\_admin controller and pointed it to my  
players table. Worked great, I’m off and running.

After learning about hashing the password, I decided I might like to add  
a convenience method to the Player (attached) which would give fullname.  
My ultimate goal of this is to list the players, obviously.

In the player\_admin/show view, I tried \<%= @player.fullname %\>, got an  
error which said fullname not defined. I also just dumped the player  
object using debug(), which didn’t show fullname as an attribute.

I also tried it as a no-param method [i.e., fullname()], without luck.

After puzzling a while, I came across the notion of helpers, and decided  
to try the method in the player\_admin\_helper:

def fullname  
@player.first\_name + " " + @player.last\_name  
end

Worked like a charm. I kind of understand, it’s because the show view  
was invoked by the player\_admin controller, so he has access to the  
player\_admin\_helper, but that won’t help me when I’m off in some other  
part of the application.

I don’t understand where I went wrong when just trying to get a similar  
method directly in the player object. Is there something 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.

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 28, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/beginner-helper-vs-model/154403/2 "2008-11-28T20:05:32Z")

</div>

On Nov 28, 2008, at 1:51 PM, Scott P. 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

> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 28, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/beginner-helper-vs-model/154403/3 "2008-11-28T20:35:45Z")

</div>

Rob B. wrote:

> On Nov 28, 2008, at 1:51 PM, Scott P. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

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.
