I have been following a few Rails Beginner tutorials and have been able to get a good simple app up where I have a bunch of servers and I am tracking the number of users on those servers every week. I have been able to get the app running so that I can view the user counts for a Server - looks like this, which is displayed via the server model''s show.rhtml: ================Server Name: EastWS2003 UserCounts: 300 302 305 315 ================ So far so good, but rather than have that usercount table store the date of that report I have a third table that consists of an id field and a field called "weekending", which stores that actual date. The usercount table has a field called reportdate_id which points to the reportdate table. The result I want is to see something like this: ================Server: NorthWM2k UserCounts: 01/01/2007 300 01/08/2007 302 01/15/2007 305 01/22/2007 315 ================ However I can''t figure out how to code this into the show.rhtml to pull in the actual date from the reportdate table. Here is the original code from show.rhtml that displays the user count. <% if @server.has_usercounts? %> <h3>User Counts</h3> <table> <% for usercount in @server.usercounts %> <tr> <td><%= usercount.users %></td> </tr> <% end %> </table> <% end %> Can anyone point me in the right direction? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 6, 2007, at 9:01 AM, Stoop wrote:> I have been following a few Rails Beginner tutorials and have been > able to get a good simple app up where I have a bunch of servers and I > am tracking the number of users on those servers every week. > > I have been able to get the app running so that I can view the user > counts for a Server - looks like this, which is displayed via the > server model''s show.rhtml: > ================> Server Name: EastWS2003 > > UserCounts: > > 300 > 302 > 305 > 315 > ================> > So far so good, but rather than have that usercount table store the > date of that report I have a third table that consists of an id field > and a field called "weekending", which stores that actual date. > > The usercount table has a field called reportdate_id which points to > the reportdate table. > > The result I want is to see something like this: > > ================> Server: NorthWM2k > > UserCounts: > > 01/01/2007 300 > 01/08/2007 302 > 01/15/2007 305 > 01/22/2007 315 > ================> > However I can''t figure out how to code this into the show.rhtml to > pull in the actual date from the reportdate table. Here is the > original code from show.rhtml that displays the user count. > > <% if @server.has_usercounts? %> > <h3>User Counts</h3> > <table> > <% for usercount in @server.usercounts %> > <tr><td><%= usercount.reportdate.weekending.strftime(''%m/%d/%Y'') %></td>> <td><%= usercount.users %></td> > </tr> > <% end %> > </table> > <% end %> > > Can anyone point me in the right direction?Assuming that your models reflect the associations that you implied (and that I''m interpreting them right). class Server has_many :usercounts end class Usercount belongs_to :server belongs_to :reportdate end class Reportdate set_table_name ''reportdate'' # because you imply it''s not plural has_many :usercounts end But if this is a common thing, I''d suggest you also have a method: class Usercount def weekending self.reportdate.weekending end end Define your own format (in your environment.rb perhaps) Time::DATE_FORMATS[:stoop] = ''%m/%d/%Y'' and in your view say: <td><%= usercount.weekending.to_s(:stoop) %></td> Of course, you can replace :stoop with :mdy or anything else that suits you. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That was it, perfect Rob, thanks very much. On Sep 6, 9:55 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Sep 6, 2007, at 9:01 AM, Stoop wrote:> I have been following a few Rails Beginner tutorials and have been > > able to get a good simple app up where I have a bunch of servers and I > > am tracking the number of users on those servers every week. > > > I have been able to get the app running so that I can view the user > > counts for a Server - looks like this, which is displayed via the > > server model''s show.rhtml: > > ================> > Server Name: EastWS2003 > > > UserCounts: > > > 300 > > 302 > > 305 > > 315 > > ================> > > So far so good, but rather than have that usercount table store the > > date of that report I have a third table that consists of an id field > > and a field called "weekending", which stores that actual date. > > > The usercount table has a field called reportdate_id which points to > > the reportdate table. > > > The result I want is to see something like this: > > > ================> > Server: NorthWM2k > > > UserCounts: > > > 01/01/2007 300 > > 01/08/2007 302 > > 01/15/2007 305 > > 01/22/2007 315 > > ================> > > However I can''t figure out how to code this into the show.rhtml to > > pull in the actual date from the reportdate table. Here is the > > original code from show.rhtml that displays the user count. > > > <% if @server.has_usercounts? %> > > <h3>User Counts</h3> > > <table> > > <% for usercount in @server.usercounts %> > > <tr> > > <td><%= usercount.reportdate.weekending.strftime(''%m/%d/%Y'') %></td> > > > <td><%= usercount.users %></td> > > </tr> > > <% end %> > > </table> > > <% end %> > > > Can anyone point me in the right direction? > > Assuming that your models reflect the associations that you implied > (and that I''m interpreting them right). > > class Server > has_many :usercounts > end > class Usercount > belongs_to :server > belongs_to :reportdate > end > class Reportdate > set_table_name ''reportdate'' # because you imply it''s not plural > has_many :usercounts > end > > But if this is a common thing, I''d suggest you also have a method: > > class Usercount > def weekending > self.reportdate.weekending > end > end > > Define your own format (in your environment.rb perhaps) > > Time::DATE_FORMATS[:stoop] = ''%m/%d/%Y'' > > and in your view say: > > <td><%= usercount.weekending.to_s(:stoop) %></td> > > Of course, you can replace :stoop with :mdy or anything else that > suits you. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob helped me out and got me to the stage where I have the following output in show.rhtml ================Server: NorthWM2k UserCounts: 01/01/2007 300 01/08/2007 302 01/15/2007 305 01/22/2007 315 ================ I modified this a bit because I wanted to be able to create new user counts at the bottom of the document as follows: ================Server: NorthWM2k UserCounts: 01/01/2007 300 01/08/2007 302 01/15/2007 305 01/22/2007 315 ================Date: ________ Users: _________ I managed to get it to work OK if I enter the ID number of reportdate, followed by the usercount. However typing the number 39 instead of choosing 2007-08-31 from a drop down is not good GUI!!!! I am really struggling with the select statement that will allow me to choose an existing date from the reportdate table (displaying the weekending field). In the cookbook application there is this code to display a category to choose from in that application as follows: <select name="recipe[category_id]"> <% @categories.each do |category| %> <option value="<%= category.id %>"> <%= category.name %> </option> <% end %> </select> So I am trying to map that to my server, usercount and reportdate table and figure out what needs to go where. Can anyone help me figure out how to populate the Date field on the form with the contents of the reportdate table, preferably having as a default value the last item added to the reportdate table? I''m not sure whether reportdate stands on its own here or whether I can only refer to it via the usercount table. Thanks for the help. On Sep 6, 9:55 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Sep 6, 2007, at 9:01 AM, Stoop wrote: > > > I have been following a few Rails Beginner tutorials and have been > > able to get a good simple app up where I have a bunch of servers and I > > am tracking the number of users on those servers every week. > > > I have been able to get the app running so that I can view the user > > counts for a Server - looks like this, which is displayed via the > > server model''s show.rhtml: > > ================> > Server Name: EastWS2003 > > > UserCounts: > > > 300 > > 302 > > 305 > > 315 > > ================> > > So far so good, but rather than have that usercount table store the > > date of that report I have a third table that consists of an id field > > and a field called "weekending", which stores that actual date. > > > The usercount table has a field called reportdate_id which points to > > the reportdate table. > > > The result I want is to see something like this: > > > ================> > Server: NorthWM2k > > > UserCounts: > > > 01/01/2007 300 > > 01/08/2007 302 > > 01/15/2007 305 > > 01/22/2007 315 > > ================> > > However I can''t figure out how to code this into the show.rhtml to > > pull in the actual date from the reportdate table. Here is the > > original code from show.rhtml that displays the user count. > > > <% if @server.has_usercounts? %> > > <h3>User Counts</h3> > > <table> > > <% for usercount in @server.usercounts %> > > <tr> > > <td><%= usercount.reportdate.weekending.strftime(''%m/%d/%Y'') %></td> > > > <td><%= usercount.users %></td> > > </tr> > > <% end %> > > </table> > > <% end %> > > > Can anyone point me in the right direction? > > Assuming that your models reflect the associations that you implied > (and that I''m interpreting them right). > > class Server > has_many :usercounts > end > class Usercount > belongs_to :server > belongs_to :reportdate > end > class Reportdate > set_table_name ''reportdate'' # because you imply it''s not plural > has_many :usercounts > end > > But if this is a common thing, I''d suggest you also have a method: > > class Usercount > def weekending > self.reportdate.weekending > end > end > > Define your own format (in your environment.rb perhaps) > > Time::DATE_FORMATS[:stoop] = ''%m/%d/%Y'' > > and in your view say: > > <td><%= usercount.weekending.to_s(:stoop) %></td> > > Of course, you can replace :stoop with :mdy or anything else that > suits you. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---