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