Beginner: helper vs model

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.

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

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

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.

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@AgileConsultingLLC.com