# What am i doing?

**URL:** <https://www.ruby-forum.com/t/what-am-i-doing/147896>\
**Category:** Rails\
**Created:** [September 6, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/what-am-i-doing/147896 "2008-09-06T18:49:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cherok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@cherok](https://www.ruby-forum.com/u/cherok)\
**Post date:** [September 6, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/what-am-i-doing/147896/1 "2008-09-06T18:49:10Z")

</div>

this is my first application in RoR and am kind of stuck. part of my  
confusion is where/when to use the model, controller or view.

one of my tables is called gi\_maps which is basically a hash:

sqlite\> select \* from gi\_maps;  
id|name  
1|Gi  
2|No-Gi

another is called divisions which has gi\_maps hash and others:

sqlite\> select \* from divisions;  
id|gi\_map\_id|rank\_map\_id|weight\_group\_map\_id|age\_group\_map\_id|  
created\_at|updated  
\_at  
3|2|4|4|3|2008-09-05 21:28:32|2008-09-05 21:28:32  
4|1|6|4|3|2008-09-05 21:29:01|2008-09-05 21:29:01

my default show “view” (divisions/show.html.erb) just displays the  
id’s for each entry:

Gi or No-Gi Rank Weight group Age group  
2 4 4 3 Show Edit Destroy  
1 6 4 3 Show Edit Destroy

divisions/show.html.erb:  
.  
.

**Gi or No-Gi:** \<%=h @division.gi\_map\_id %\>

. . .

so all of this works fine… except for the fact that people like to  
see words instead of numbers 🙂 so what i’ve tried to do is use my  
model (models/division.rb) to “collect” the corresponding “name” value  
for a given hash id (ie. gi\_map\_id). but for some reason this isn’t  
working and i can’t think of any other way to do it except for placing  
it in the controller or view section. but i _think_ this should be  
done at the model level.

here are the files and errors the way i’m doing it (i’ll truncate the  
files for readability):

models/gi\_map.rb:

class GiMap \< ActiveRecord::Base  
belongs\_to :divison  
end

models/division.rb:

class Division \< ActiveRecord::Base

belongs\_to :match

def self.gi\_map\_name  
GiMap.first(:conditions =\> [“id = ?”, self.id]).name  
end  
end

controllers/divisions\_controller.rb:  
.  
.  
def show  
@division = Division.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @division }
end

```

end  
.  
.

views/divisons/show.html.rb:  
.  
.

**Gi or No-Gi:** \<%=h @division.gi\_map\_name %\>

. . .

and here is the error when trying to “show” one record:

NoMethodError in Divisions#show

Showing divisions/show.html.erb where line #3 raised:

undefined method `gi\_map\_name’ for #Division:0x478b9b0

Extracted source (around line #3):

1:

2: **Gi or No-Gi:**  
3: \<%=h @division.gi\_map\_name %\>  
4:

  
5:  
6: 

i hope this isn’t a total noob question! but i’ve already written  
this application in php/mysql and am just rewriting in an effort to  
learn RoR. so any help or tips would be greatly appreciated.

thanks!

ron

---

<div class="post-metadata">

**Author:** ![cherok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@cherok](https://www.ruby-forum.com/u/cherok)\
**Post date:** [September 6, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/what-am-i-doing/147896/2 "2008-09-06T18:51:31Z")

</div>

Defining methods with the self prefix denotes a class method. You are  
calling the method on an instance object. Remove the self and you will  
be  
good to go.

---

<div class="post-metadata">

**Author:** ![cherok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@cherok](https://www.ruby-forum.com/u/cherok)\
**Post date:** [September 6, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/what-am-i-doing/147896/3 "2008-09-06T19:19:03Z")

</div>

wow! i was pretty far off 🙂 thanks for the tips! i would like to  
know more about when/where and what the implications are for using the  
has\_one, belongs\_to, etc. keywords. any links that would help me  
better understand what the hell i’m doing ? 🙂

btw… i DO get what you are saying… after changing the belongs\_to  
and has\_one… i can just do this in my view:

**Gi or No-Gi:** \<%=h @division.gi\_map.name %\>

thanks again!

On Sep 6, 12:53 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cherok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@cherok](https://www.ruby-forum.com/u/cherok)\
**Post date:** [September 6, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/what-am-i-doing/147896/4 "2008-09-06T18:54:27Z")

</div>

On Sep 6, 5:36 pm, Ron DeMeritt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> this is my first application in RoR and am kind of stuck. part of my  
> confusion is where/when to use the model, controller or view.

class Division \< ActiveRecord::Base

> belongs\_to :match
> 
> def self.gi\_map\_name  
> GiMap.first(:conditions =\> [“id = ?”, self.id]).name  
> end  
> end

You’ve created a class method (that’s what def self. does) whereas  
this should really be an instance method.  
Usually you’d handle this via some associations. GiMap should have  
has\_one :division (and not belongs\_to :division - that would indicate  
that the gi\_maps table has a division\_id column) and division should  
belongs\_to :gi\_map (and it does as required have a gi\_map\_id column).

Then you’d just write (I suspect that personally I wouldn’t bother)

def gi\_map\_name  
gi\_map.name  
end

Fred

---

<div class="post-metadata">

**Author:** ![cherok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@cherok](https://www.ruby-forum.com/u/cherok)\
**Post date:** [September 7, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/what-am-i-doing/147896/5 "2008-09-07T17:10:04Z")

</div>

> **[\#3 Find Through Association - RailsCasts](http://railscasts.com/episodes/3-find-through-association)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
