What am i doing?

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 :slight_smile: 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

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.

wow! i was pretty far off :slight_smile: 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 ? :slight_smile:

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. [email protected]

On Sep 6, 5:36 pm, Ron DeMeritt [email protected] 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