I finally found a solution to my problem yesterday, mostly user error. I just wasn''t thinking in the right mindset. Thanks for all the help, it pushed me towards the solution. Now I have another problem, when I am printing out the listing of Scenarios my boss wants the id fields that point to other tables to show the label instead of the id number. table: Scenarios id => autoint case_number => varchar swversion_id => int ... ex: id case_number swversion_id ... 1 aaaaa12345 2 ... 2 bbbbb67890 1 ... table: Swversions id => autoint sw_version => varchar ex: id sw_version 1 version1 2 version2 the results of my list.rhtml currently display: case_number swversion_id ... for example: aaaaa12345 2 ... bbbbb67890 1 ... what I want to display is: case_number sw_version aaaaa12345 version2 ... bbbbb67890 version1 ... to me it makes sense that I should be able to set up some code like this assuming @swvers=Swversion.find_all ... <table> <tr> <th>Case Numer</th> <th>SW Version</th> <th>...</th> </tr> <% for scenario in @scenarios %> <tr> <td><%=h scenario.case_number %> <td><%=h what to put here? @swvers[scenario.swversion_id].sw_version ?? %></td> <td>...</td> </tr> </table> ... But of course that doesn''t work. I am having a hard time grasping the variable structure that Rails uses, so I am fighting with Rails until it dawns on me what I am doing wrong. - Michael
Michael King wrote:>I finally found a solution to my problem yesterday, mostly user error. >I just wasn''t thinking in the right mindset. Thanks for all the help, >it pushed me towards the solution. > >Now I have another problem, when I am printing out the listing of >Scenarios my boss wants the id fields that point to other tables to >show the label instead of the id number. > >table: Scenarios >id => autoint >case_number => varchar >swversion_id => int >... > >ex: >id case_number swversion_id ... >1 aaaaa12345 2 ... >2 bbbbb67890 1 ... > > >table: Swversions >id => autoint >sw_version => varchar > >ex: >id sw_version >1 version1 >2 version2 > >the results of my list.rhtml currently display: >case_number swversion_id ... > >for example: >aaaaa12345 2 ... >bbbbb67890 1 ... > >what I want to display is: >case_number sw_version >aaaaa12345 version2 ... >bbbbb67890 version1 ... > >to me it makes sense that I should be able to set up some code like this >assuming @swvers=Swversion.find_all >... ><table> ><tr> ><th>Case Numer</th> ><th>SW Version</th> ><th>...</th> ></tr> ><% for scenario in @scenarios %> ><tr> ><td><%=h scenario.case_number %> ><td><%=h what to put here? @swvers[scenario.swversion_id].sw_version ?? %></td> ><td>...</td> ></tr> ></table> >... > >But of course that doesn''t work. I am having a hard time grasping the >variable structure that Rails uses, so I am fighting with Rails until >it dawns on me what I am doing wrong. > >1. you probably want to use eager loading of the association 2. scenario has_one swversion association should be setup 3. <% for scenario in @scenarios %> <tr> <td><%=h scenario.case_number %> <td><%=h scenario.swversion.sw_version %></td> <td>...</td> </tr> <% end %> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
You really need to read this: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails