Michael King
2005-Sep-16 15:29 UTC
Problem accessing a field 2 tables away in an rhtml file
I am having a problem displaying the 3rd level field teamname in a
rhtml file, while I can display the 2nd level fields panic_code and
panic_name. The code snippet is
...
<% for scenario in @scenarios %>
<tr>
<td><% unless scenario.panicname_id == nil -%>
<%=h scenario.panicname.panic_code %> <%=h
scenario.panicname.panic_name %>
<% end -%>
</td>
<td><% unless scenario.note == nil -%>
<% unless scenario.note.team_id == 1 -%>
<%=h scenario.note.team.teamname %>
<% end -%>
<% end -%>
</td>
</tr>
....
For the life of me I can''t figure out what I am doing wrong.
- Michael
Relavent Schema
Scenarios:
panicname_id int
Panicnames:
panic_code char(10)
panic_name varchar(45)
Notes:
scenario_id int
team_id int
Teams:
teamname varchar(45)
Relavent Models:
Scenario:
belongs_to :panicname
has_many :note
Panicname:
has_many :scenario
Note:
belongs_to :scenario
belongs_to :team
Team:
has_many :note
Andrew Stone
2005-Sep-16 16:47 UTC
Re: Problem accessing a field 2 tables away in an rhtml file
scenario.note (from the has_many :note declaration in the scenario model)
therefore you''re trying to access a field of an array. you need to get
the
note from the array to access the field.
so something like
<%
scenario.note.each{|note|
unless (note.team.team_id ==1)
%>
<%=h note.team.teamname%>
<%
end
}
%>
--
Andrew Stone
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails