# Calling a method from a different class's view

**URL:** <https://www.ruby-forum.com/t/calling-a-method-from-a-different-classs-view/185374>\
**Category:** Rails\
**Created:** [March 18, 2010, 11:27pm UTC](https://www.ruby-forum.com/t/calling-a-method-from-a-different-classs-view/185374 "2010-03-18T23:27:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@stu](https://www.ruby-forum.com/u/stu)\
**Post date:** [March 18, 2010, 11:27pm UTC](https://www.ruby-forum.com/t/calling-a-method-from-a-different-classs-view/185374/1 "2010-03-18T23:27:41Z")

</div>

Hi all,

I have two classes: a player class and a game class. Each game has two  
player IDs. I would like the game show view to be able to display each  
player’s full name via a method, as follows:  
\<%=h Player.get\_player\_name(@game.player2) %\>

Here is the method definition in my players controller:  
def get\_player\_name(playerID)  
@player = Player.find(playerID)  
@playerName = @player.firstName + " " + @player.lastName  
end

When I try to view the page, I get the error:  
Showing app/views/games/show.html.erb where line #8 raised:

undefined method `get\_player\_name’ for #Class:0x252de68

Extracted source (around line #8):

5:  
6:

7: **Player2:**  
8: \<%=h Player.get\_player\_name(@game.player2) %\>  
9:

  
10:  
11: 

I can call the get\_player\_name method from a player view. Clearly I  
don’t understand something about how rails does its scoping, but I am  
not sure where to look. For instance, I don’t see anything in my  
Learning Rails book that might explain how this works.

My question is: where can I put my get\_player\_name method so that it  
can be accessed by other classes as well?

I am actually surprised that I can’t access the method, because the  
following does work from within my game show view:  
\<%=h Player.find(@game.player1).firstName + " " +  
Player.find(@game.player1).lastName %\>

Thanks,  
Stu

---

<div class="post-metadata">

**Author:** ![stu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@stu](https://www.ruby-forum.com/u/stu)\
**Post date:** [March 18, 2010, 11:33pm UTC](https://www.ruby-forum.com/t/calling-a-method-from-a-different-classs-view/185374/2 "2010-03-18T23:33:32Z")

</div>

On Mar 18, 10:03 pm, Stu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 8: \<%=h Player.get\_player\_name(@game.player2) %\>  
> can be accessed by other classes as well?

This is more of a ruby / object oriented programming fundamentals  
question. You say that you defined this method in the players  
controller, but you’re attempting to call it as if it was a class  
method on Player. If you wanted to make this work then you’d want to  
actually make it a class method on Player, ie you would write

def.self.get\_player\_name(…)  
…  
end

in your player class.

That would be slightly odd design though. A more railsy way of doing  
it would be for player2 to be an association and to define a full name  
method on Player ie

def full\_name  
firstName + lastName  
end

and then your view would look like \<%= h @game.player2.full\_name %\>

It’s also convention that attribute names be underscored (ie  
first\_name rather than firstName)

Fred

---

<div class="post-metadata">

**Author:** ![stu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@stu](https://www.ruby-forum.com/u/stu)\
**Post date:** [March 19, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/calling-a-method-from-a-different-classs-view/185374/3 "2010-03-19T14:28:28Z")

</div>

On Mar 18, 5:32 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It’s also convention that attribute names be underscored (ie  
> first\_name rather than firstName)

The other convention I think is important here is that your “getters”  
do not start with “get” like they do in Java. You generally just put  
the attribute name. Because Ruby doesn’t require parentheses after  
methods, you can just say “player.full\_name” instead of  
“player.getFullName()” like you would in Java. It’s a more concise and  
clear grammar.
