Hi Guys, Another (hopefully) simple problem, but I''m finding little help on any tutorials. I currently have 2 tables, one with the users of a system and another with the details of the program they are on, the program is referenced in the users table with program_id. Rails obviously hides this information in the list view as it is an id, but i actually want it to cross reference the programs table and display the program name (rather than the id, or nothing). I''ve set the model up so that each user belongs to a program, and each program has many users, and the edit page gives an option list with the names of the programs and saves the id as you''d expect I''ve seen one example that says simply writing program.name will do this but I''ve had no joy... can someone shed some light/make it blindingly obvious what I''m missing? thanks. in case what I''ve written makes absolutely no sense, the offending application can be found here: http://spotter.derbyproject.com/users/list as you can see, the users program is not displayed, but the edit page allows the edit. I need to display the name of the program in the list. cheers, Lee -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Rails obviously hides this information in the list view as it is anid,> but i actually want it to cross reference the programs table and display > the program name (rather than the id, or nothing)....> I''ve seen one example that says simply writing program.name will do this > but I''ve had no joy... can someone shed some light/make it blindingly > obvious what I''m missing? thanks.Sounds (and looks) like you used the standard scaffolding to create the view... Use user.programm.name (or whatever the name of the loop variable is instead of "user") Also, i would suggest to use eager loading to keep that line of code above from repeatedly hitting the Database to fetch the programm name: def index @users = User.find :all, :include => :programm end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Thorsten, You''re right, I''m using standard scaffold code at this point, attempting to work from it. Have a couple of questions, at present, the loop writing the show table looks like this <table> <tr> <% for column in User.content_columns %> <th><%= column.human_name %></th> <% end %> </tr> <% for user in @users %> <tr> <% for column in User.content_columns %> <td><%=h user.send(column.name) %></td> <% end %> <td><%= link_to ''Show'', :action => ''show'', :id => user %></td> <td><%= link_to ''Edit'', :action => ''edit'', :id => user %></td> <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => user }, :confirm => ''Are you sure?'', :method => :post %></td> </tr> <% end %> </table> So to display the program name as discussed would be as simple as an if statement checking if it has come to the program column yet? or do i need to hardcode the table columns? I''m sorry these are pretty simple questions, I''m used to more conventional JSP/CF scripting and ruby is a bit left-field for me! Thanks! Lee. On Apr 14, 12:10 pm, Thorsten <duple...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > Rails obviously hides this information in the list view as it is an > id, > > > but i actually want it to cross reference the programs table and display > > the program name (rather than the id, or nothing). > ... > > I''ve seen one example that says simply writing program.name will do this > > but I''ve had no joy... can someone shed some light/make it blindingly > > obvious what I''m missing? thanks. > > Sounds (and looks) like you used the standard scaffolding to create > the view... > > Use user.programm.name (or whatever the name of the loop variable is > instead of "user") > > Also, i would suggest to use eager loading to keep that line of code > above from repeatedly hitting the Database to fetch the programm name: > > def index > @users = User.find :all, :include => :programm > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I wasn''t too clear up there, Thorsten was quite right and the code worked (thanks!) I''m just looking for the best way to go about implementing it, ideally I don''t want to loose the way the app currently gets the column names dynamically without me having to hard code them. Cheers, Lee. On Apr 14, 7:34 pm, lee farrar <l.m.far...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Thanks Thorsten, > > You''re right, I''m using standard scaffold code at this point, > attempting to work from it. > Have a couple of questions, at present, the loop writing the show > table looks like this > > <table> > <tr> > <% for column in User.content_columns %> > <th><%= column.human_name %></th> > <% end %> > </tr> > > <% for user in @users %> > <tr> > <% for column in User.content_columns %> > <td><%=h user.send(column.name) %></td> > <% end %> > > <td><%= link_to ''Show'', :action => ''show'', :id => user %></td> > <td><%= link_to ''Edit'', :action => ''edit'', :id => user %></td> > <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => > user }, :confirm => ''Are you sure?'', :method => :post %></td> > </tr> > <% end %> > > </table> > > So to display the program name as discussed would be as simple as an > if statement checking if it has come to the program column yet? or do > i need to hardcode the table columns? > > I''m sorry these are pretty simple questions, I''m used to more > conventional JSP/CF scripting and ruby is a bit left-field for me! > > Thanks! > Lee. > > On Apr 14, 12:10 pm, Thorsten <duple...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > Rails obviously hides this information in the list view as it is an > > id, > > > > but i actually want it to cross reference the programs table and display > > > the program name (rather than the id, or nothing). > > ... > > > I''ve seen one example that says simply writing program.name will do this > > > but I''ve had no joy... can someone shed some light/make it blindingly > > > obvious what I''m missing? thanks. > > > Sounds (and looks) like you used the standard scaffolding to create > > the view... > > > Use user.programm.name (or whatever the name of the loop variable is > > instead of "user") > > > Also, i would suggest to use eager loading to keep that line of code > > above from repeatedly hitting the Database to fetch the programm name: > > > def index > > @users = User.find :all, :include => :programm > > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---