POST User-Agent: Xnntp/beta03 (PPC Mac OS 10.3) i''m implementing a help ticket-type system in rails, and i''m running into a problem getting data from related tables to display properly in a partial. first, the tables... a "requests" table with: id date_created created_by issue_description ... status_id which contains data for a specific request ( date, issue, etc) then a "statuscodes" table with: id status status_description which contains a list of possible states the issue is in ( in queue, in progress, on hold, complete, etc) i want to model the data so that a particular status code is associated with many requests, ie, show me all of the requests with ''on hold'' status. so... request.rb (excerpt): belongs_to :statuscode statuscode.rb: has_many :buildrequests this setup works perfectly for new records and editing existing records from the form partial: <%= options_from_collection_for_select @statuscodes, "id", "status", @request.status_id %> in the form partial gives a nice dropdown box with all of the ''status'' entries from the statuscodes table. in the list partial, i want to display the list of requests with their associated status. the partial is called from the list.rhtml view via: <%= render_collection_of_partials "list_rows", @requests %> in the code for the _list_rows.rhtml partial i have: <td><%= list_rows["status_id"] %></td>, which outputs the *id* of the associated statuscode, but i want it to print the actual *text* code, ie ''in queue'' or ''on hold''. following the 4 Days on Rails tutorial, pg. 20, i tried: <td><%= list_rows.status["status"] %></td> which produced: undefined method `status'' for #<Request:0x26bbe34> then i tried: <td><%= list_rows.status_id["status"] %></td> which gave: cannot convert String into Integer i''m obviously missing something simple here, and i think it''s the fact that i can''t quite wrap my brain around two important concepts: one is the proper way to define relationships (or ''associations'') in rails, and the second is the business of passing variables from controller to view. I''m not sure I understand the concept of an ''instance'' variable as opposed to a ''local'' variable, even though it seemed pretty intuitive when i was going through the pickaxe book. any pointers to help out a very frustrated, slightly clueless (but trying!) newbie would be appreciated!
On Sun, 2005-05-01 at 21:36 +0000, lester bangs wrote:> i''m implementing a help ticket-type system in rails, and i''m running into a > problem getting data from related tables to display properly in a partial. > > first, the tables... a "requests" table with: > id > date_created > created_by > issue_description > ... > status_id > which contains data for a specific request ( date, issue, etc) > > then a "statuscodes" table with: > id > status > status_description > which contains a list of possible states the issue is in ( in queue, in > progress, on hold, complete, etc)What is the difference between status and status_description here? I see you want to mark a ticked "in progress", "complete",etc. - this could be held in status. Will you be using the status_description field?> > i want to model the data so that a particular status code is associated with > many requests, ie, show me all of the requests with ''on hold'' status. so... > > request.rb (excerpt): > belongs_to :statuscodesounds reasonable> statuscode.rb: > has_many :buildrequestsI don''t think this is really what you want here. Any one request should be related to one status code, so the relationship set in request.rb should be all you need.> this setup works perfectly for new records and editing existing records from > the form partial:> in the list partial, i want to display the list of requests with their > associated status. the partial is called from the list.rhtml view via: > > <%= render_collection_of_partials "list_rows", @requests %> > > in the code for the _list_rows.rhtml partial i have: > > <td><%= list_rows["status_id"] %></td>, > > which outputs the *id* of the associated statuscode,This is the status_id field of your requests table-- as one would expect.> but i want it to print > the actual *text* code, ie ''in queue'' or ''on hold''.I would think it would be <%= list_rows.statuscode.status %> But, I too am a noob and your naming convention has confused me a bit. I was under the impression that for the Rails automagic to work you would need to name your foreign key field in requests: statuscode_id. So, I''m baffled myself as to why your new and edit methods are working as well as they are. Hopefully,someone can enlighten me here?> and the second is the > business of passing variables from controller to view. I''m not sure I > understand the concept of an ''instance'' variable as opposed to a ''local'' > variable, even though it seemed pretty intuitive when i was going through the > pickaxe book.I was going to take a stab here, but the language stated having my mind wraping back around on itself before I could get it coherant.> any pointers to help out a very frustrated, slightly clueless (but trying!) newbie would be appreciated!Join the club! HTH, Howard
> -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of lester bangs > Sent: Sunday, May 01, 2005 5:36 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] lookup tables in partials > > POST > User-Agent: Xnntp/beta03 (PPC Mac OS 10.3) > > i''m implementing a help ticket-type system in rails, and i''m runninginto> a > problem getting data from related tables to display properly in apartial.> > first, the tables... a "requests" table with: > id > date_created > created_by > issue_description > ... > status_id > which contains data for a specific request ( date, issue, etc) > > then a "statuscodes" table with: > id > status > status_description > which contains a list of possible states the issue is in ( in queue,in> progress, on hold, complete, etc) > > i want to model the data so that a particular status code isassociated> with > many requests, ie, show me all of the requests with ''on hold'' status. > so... > > request.rb (excerpt): > belongs_to :statuscode > > statuscode.rb: > has_many :buildrequests > > this setup works perfectly for new records and editing existingrecords> from > the form partial: > <%= options_from_collection_for_select @statuscodes, "id", "status", > @request.status_id %> in the form partial gives a nice dropdown boxwith> all > of the ''status'' entries from the statuscodes table. > > in the list partial, i want to display the list of requests with their > associated status. the partial is called from the list.rhtml view via: > > <%= render_collection_of_partials "list_rows", @requests %> > > in the code for the _list_rows.rhtml partial i have: > > <td><%= list_rows["status_id"] %></td>, > > which outputs the *id* of the associated statuscode, but i want it to > print > the actual *text* code, ie ''in queue'' or ''on hold''. following the 4Days> on > Rails tutorial, pg. 20, i tried: > > <td><%= list_rows.status["status"] %></td> > > which produced: > undefined method `status'' for #<Request:0x26bbe34>This is fairly simple. There''s some naming conventions that you aren''t really adhering to, so both you and rails are getting a little confused. Here''s what you do sticking to your naming as much as I can... First, the "status_id" field in the table "requests" should be named "statuscode_id". This is because the table you''re trying to link to is called "statuscodes" not "statuses". Next, when you want to reference the associate statuscode, you''d use "list_rows.statuscode". And when you want to reference the "status" field on the "statuscode" of the "request" as you mention above, you''d do: <td><%= list_rows.statuscode.status %></td> Now for the reverse direction of the association: "statuscode.rb" should have the association set up as "has_many :requests", since the table you are linking to is called "requests" (and not "buildrequests", you can still say the association should be named "buildrequests", but then you have to tell rails what table you''re really linking to. I suggest ignoring that for now until you are more familiar with rails). Now for some general advice: You may want to rename the table "statuscodes" to "status_codes". Then the model class will be called StatusCode. This follows the general convention that underscores separate words inside the database (to make it easier to read), the class names will be in CamelCase and the method/fields names in Ruby will use the underscores (both ruby coding conventions). This leads us to have the field named "status_code_id" inside the "requests" table (instead of "status_id" or "statuscode_id", and to make the association set up as " belongs_to :status_code". Now when we have a request and want to reference its status code, we do "requests.status_code" (or as in the above example "list_rows.status_code") Thanks, Chris
> On Sun, 2005-05-01 at 21:36 +0000, lester bangs wrote: > >> i''m implementing a help ticket-type system in rails, and i''m running >> into a problem getting data from related tables to display properly >> in a partial. >> >> first, the tables... a "requests" table with: >> id >> date_created >> created_by >> issue_description >> ... >> status_id >> which contains data for a specific request ( date, issue, etc) >> then a "statuscodes" table with: >> id >> status >> status_description >> which contains a list of possible states the issue is in ( in queue, >> in >> progress, on hold, complete, etc)> What is the difference between status and status_description here? I > see you want to mark a ticked "in progress", "complete",etc. - this > could be held in status. Will you be using the status_description > field? >yes, the ''status'' field contains the actual status levels a request can have; in queue, in progress, complete, etc. the ''status_description'' is a text field that contains a more verbose description of a particular status. it''s not specifically necessary, but i thought it might be a good idea to add it and implement a pop-up help box so the user can understand, say, what the difference is between a ''waiting'' status and an ''in queue'' status.>> i want to model the data so that a particular status code is >> associated with many requests, ie, show me all of the requests with >> ''on hold'' status. so... >> >> request.rb (excerpt): >> belongs_to :statuscode > sounds reasonable > >> statuscode.rb: >> has_many :buildrequests > I don''t think this is really what you want here. Any one request > should be related to one status code, so the relationship set in > request.rb should be all you need.so ''belongs_to :statuscode'' in request.rb is enough to establish a relationship between a request and a status code? will i be able to query the statuscode table for, say, all the ''in progress'' status codes and get back all the requests with that status?> >> this setup works perfectly for new records and editing existing >> records from the form partial: >> >> in the list partial, i want to display the list of requests with >> their associated status. the partial is called from the list.rhtml >> view via: >> >> <%= render_collection_of_partials "list_rows", @requests %> >> >> in the code for the _list_rows.rhtml partial i have: >> >> <td><%= list_rows["status_id"] %></td>, >> >> which outputs the *id* of the associated statuscode, >> > This is the status_id field of your requests table-- as one would > expect. > >> but i want it to print the actual *text* code, ie ''in queue'' or ''on >> hold''. >> > I would think it would be <%= list_rows.statuscode.status %> >gave that a shot, but I''m still getting errors: NoMethodError in Buildrequests#list Showing /requests/_list_rows.rhtml where line #5 raised: undefined method `status'' for nil:NilClass> But, I too am a noob and your naming convention has confused me a bit. > > I was under the impression that for the Rails automagic to work you > would need to name your foreign key field in requests: statuscode_id. > So, I''m baffled myself as to why your new and edit methods are working > as well as they are. Hopefully,someone can enlighten me here?not sure either, but it works. all the records from the ''status'' field come up just fine in a nicely populated dropdown. i''ll keep hammering away at it. thanks for the help!
>> -----Original Message----- >> From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> [mailto:rails- >> bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf >> Of lester bangs >> Sent: Sunday, May 01, 2005 5:36 PM >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: [Rails] lookup tables in partials >> POST >> User-Agent: Xnntp/beta03 (PPC Mac OS 10.3) >> i''m implementing a help ticket-type system in rails, and i''m running >> > into > >> a >> problem getting data from related tables to display properly in a > partial. > >> first, the tables... a "requests" table with: >> id >> date created >> created by >> issue description >> ... >> status id >> which contains data for a specific request ( date, issue, etc) >> then a "statuscodes" table with: >> id >> status >> status description >> which contains a list of possible states the issue is in ( in queue, > in > >> progress, on hold, complete, etc) >> >> i want to model the data so that a particular status code is >> > associated > >> with >> many requests, ie, show me all of the requests with ''on hold'' status. >> so... >> request.rb (excerpt): >> belongs to :statuscode >> statuscode.rb: >> has many :buildrequests >> this setup works perfectly for new records and editing existing >> > records > >> from >> the form partial: >> <%= options from collection for select @statuscodes, "id", "status", >> @request.status id %> in the form partial gives a nice dropdown box > with > >> all >> of the ''status'' entries from the statuscodes table. >> in the list partial, i want to display the list of requests with >> their associated status. the partial is called from the list.rhtml >> view via: >> >> <%= render collection of partials "list rows", @requests %> >> >> in the code for the list rows.rhtml partial i have: >> >> <td><%= list rows["status id"] %></td>, >> >> which outputs the *id* of the associated statuscode, but i want it to >> print >> the actual *text* code, ie ''in queue'' or ''on hold''. following the 4 > Days > >> on >> Rails tutorial, pg. 20, i tried: >> <td><%= list rows.status["status"] %></td> >> >> which produced: >> undefined method `status'' for #<Request:0x26bbe34> > This is fairly simple. There''s some naming conventions that you aren''t > really adhering to, so both you and rails are getting a little > confused. > > Here''s what you do sticking to your naming as much as I can... > > First, the "status id" field in the table "requests" should be named > "statuscode id". This is because the table you''re trying to link to is > called "statuscodes" not "statuses". > > Next, when you want to reference the associate statuscode, you''d use > "list rows.statuscode". And when you want to reference the "status" > field on the "statuscode" of the "request" as you mention above, you''d > do: > <td><%= list rows.statuscode.status %></td> > Now for the reverse direction of the association: "statuscode.rb" > should have the association set up as "has many :requests", since the > table you are linking to is called "requests" (and not > "buildrequests", you can still say the association should be named > "buildrequests", but then you have to tell rails what table you''re > really linking to. I suggest ignoring that for now until you are more > familiar with rails). > > Now for some general advice: You may want to rename the table > "statuscodes" to "status codes". Then the model class will be called > StatusCode. This follows the general convention that underscores > separate words inside the database (to make it easier to read), the > class names will be in CamelCase and the method/fields names in Ruby > will use the underscores (both ruby coding conventions). > > This leads us to have the field named "status code id" inside the > "requests" table (instead of "status id" or "statuscode id", and to > make > the association set up as " belongs to :status code". Now when we have > a > request and want to reference its status code, we do > "requests.status code" (or as in the above example > "list rows.status code")thanks so much for clearing up the naming conventions! however, i am *still* getting an error when i try to output the status field for a given status id. the line: <%= request.statuscode.id %> works perfectly, it outputs the correct status code (as a number) for a given request. however, <%= request.statuscode.status %> still barfs with: NoMethodError in Requests#list Showing /requests/list.rhtml where line #15 raised: undefined method `status'' for nil:NilClass (I pulled the code out of the partial for now and put it in the list.rhtml layout until I can get it working). Again, the new/edit views work flawlessly, correctly populating a dropdown with a list of the status codes: <select id="request_statuscode_id" name="request[statuscode_id]"> <%= options_from_collection_for_select @statuscodes, "id", "status", @request.statuscode_id %> </select> The odd thing is that the correct data seems to be getting to the layout, it just for some reason isn''t liking the way its getting it. As an example, here''s the output of a single record from the "show template parameters" when the list view fails (other records are similar): buildrequests: - !ruby/object:Request attributes: created_on: 2005-04-27 14:33:56 special_instructions: none customer_changes: none id: "7" statuscode_id: "2" submitted_on: 2005-04-27 14:33:00 submitted_by: J Smith product_name: Product statuscode: !ruby/object:Statuscode attributes: status: in queue id: "2" description: request has been processed, waiting for earlier requests to complete So like I said, the data is there and again, <%= request.statuscode.id %> works fine. As soon as I change the ''id'' to ''status'' in that line, it crashes. It appears that Rails is interpreting ''status'' as a method, but clearly from its own debug output it is an attribute. How do I tell it to treat it as such? I have *got* be missing something obvious. I have tried every iteration of the above code I could think of, with no luck whatsoever. Any additional pointers you can provide would be greatly appreciated!