Jonathan Apostoles
2005-Jul-10 20:01 UTC
Automatically populating "has_many" association with form post / ActiveRecord
I have a table that joins meta-data for record on another table. Basically I have: Item has_many :meta_tags MetaTag belongs_to :item When the form is populated, the allowed meta tags are generated as select boxes, with prepopulated options. I have a loop that uses the collection_select method to build these select boxes, one for each of the meta-data fields. How should these be named so that ActiveRecord can automatically map and knit together the has_many relationship? The collection that should be at item.meta_tags should come from the form post as MetaTag objects (which have the meta tag ID, and the value from the select box). I just can''t get this to work, and it seems pretty basic. Any ideas?
I''m sure this one was touched upon before but, I looked fairly deep into the archives and couldn''t find anything. I am running rubygems0.8.10 ruby1.8.2. I have set up my controllers as per the instructions on this site http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html but i continue to get this error whenever i browse to a page (this particular one being the default page - i have already removed the index.html file that is set up when you install rails). Illegal controller path for route default: welcome any help? thanks Josseph
Doug Alcorn
2005-Jul-10 20:57 UTC
Re: Automatically populating "has_many" association with form post / ActiveRecord
Jonathan Apostoles <jon.apostoles-1vwOZer+2/nEIuZ0ZFnJtQ@public.gmane.org> writes:> Item has_many :meta_tags > MetaTag belongs_to :item > > How should these be named so that ActiveRecord can automatically map > and knit together the has_many relationship? > > The collection that should be at item.meta_tags should come from the > form post as MetaTag objects (which have the meta tag ID, and the value > from the select box).I think Rails 0.13 now creates a item.meta_tags= method so that item.update_attributes will work as long as you pass in a list of meta_tag ids in the params[:item] hash. You can look at this page on the wiki for a similar type problem: http://wiki.rubyonrails.com/rails/show/CheckboxHABTM -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
nevermind. didn''t realize you had to run the generatioin script to make it all happen. thanks for all of your support. joseph Web sites: Jakuta & Carl - jnc.hlinak.com, Portfolio - www.hlinak.com. "You are the music while the music lasts." - T.S. Eliot>From: "joseph jakuta" <h_lina_k-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> >Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Subject: [Rails] routing problems >Date: Sun, 10 Jul 2005 15:07:45 -0500 > >I''m sure this one was touched upon before but, I looked fairly deep into >the archives and couldn''t find anything. > >I am running rubygems0.8.10 ruby1.8.2. > >I have set up my controllers as per the instructions on this site >http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html > >but i continue to get this error whenever i browse to a page (this >particular one being the default page - i have already removed the >index.html file that is set up when you install rails). >Illegal controller path for route default: welcome > >any help? >thanks >Josseph > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails
Jonathan Apostoles
2005-Jul-10 21:53 UTC
Re: Automatically populating "has_many" association with form post / ActiveRecord
I think my problem is a little more complex. Here''s some more information with a similar problem, but different application that I can more clearly explain what''s going on: When I create a new ''Inspection'', I have to look up which ''Routine'' is used, and based on that which ''InspectionPoints'' to use. Each ''InspectionPoint'' has the same possible values, retrieved from the database as InspectionFlags So the process flow is, to create a new inspection you get a form where you select which ''Routine'' to use. Based on the routine_id, it looks up the InspectionPoints that apply, and give you a form with one line per inspection point with the possible inspection flags to the right of it. So the form is kind of like this: Inspection routine: [Standard] Inspection points: - Standard check 1 : [Not inspected] - Standard check 2 : [Passed] - Standard check 3 : [Passed] [Submit] The three check selectboxes should be added to the collection within the inspection model (inspection.inspection_results) Here''s the table it should be populating: CREATE TABLE inspection_results ( inspection_id INTEGER UNSIGNED NOT NULL, inspection_point_id INTEGER UNSIGNED NOT NULL, inspection_flag_id INTEGER UNSIGNED NOT NULL ); So basically, based on those checkboxes, through the Inspection ActiveRecord, I want to populate with values of the id that just got added, followed by one line for each of the select boxes (point id, flag id). It''s creating the new record that bombs out. I''m not sure how those form elements are supposed to be named in order for ActiveRecord to automatically add all those records. On Sun, 10 Jul 2005 16:57:08 -0400 Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> Jonathan Apostoles <jon.apostoles-1vwOZer+2/nEIuZ0ZFnJtQ@public.gmane.org> writes: > > > Item has_many :meta_tags > > MetaTag belongs_to :item > > > > How should these be named so that ActiveRecord can automatically map > > and knit together the has_many relationship? > > > > The collection that should be at item.meta_tags should come from the > > form post as MetaTag objects (which have the meta tag ID, and the > > value from the select box). > > I think Rails 0.13 now creates a item.meta_tags= method so that > item.update_attributes will work as long as you pass in a list of > meta_tag ids in the params[:item] hash. You can look at this page on > the wiki for a similar type problem: > > http://wiki.rubyonrails.com/rails/show/CheckboxHABTM > -- > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jonathan Apostoles
2005-Jul-10 22:12 UTC
Re: Automatically populating "has_many" association with form post / ActiveRecord
Here''s more info that will help.. here''s what''s in my controller right now so you can see what I''m trying to do. I don''t think I should have to do this manually.. and it doesn''t work anyways. The logger output line errors out because the inspection_flag_id is of type Fixnum: ... @inspection = Inspection.new( @params[:inspection] ) @inspection.date = Time.now @inspection_results = @params[:inspection_results]; @inspection_results.each_value do |curResult| newResult = InspectionResult.new newResult.inspection_point_id = curResult["inspection_point_id"] newResult.inspection_flag_id = curResult["inspection_flag_id"] @inspection.inspection_results << newResult logger.info newResult.inspection_flag_id.description end @inspection.save end ... And here''s the form that is rendered that posts to this action. As you can see I didn''t relate that select box to the Inspection model because it seems like no matter how I did it, it wouldn''t map correctly: <table width="100%"> <tr> <th>Task</th> <th>Result</th> </tr> <% for point in @routine.inspection_points %> <tr> <td><%= point.description %></td> <td> <input type="hidden" name="inspection_results[<%= point.id %>][inspection_point_id]" value="<= point.id %>" /> <select name="inspection_results[<%= point.id %>][inspection_flag_id]"> <%= options_from_collection_for_select( @flags, :id, :description ) %> </select> </td> </tr> <% end %> </table> ---- So this doesn''t even work the way I want it to, but it''s close. The problem now is that the data set when I create a new InspectionResult in the controller is of type FixNum. Interestingly enough the correct values are set in the database this way in the InspectionResult table. - Jon On Sun, 10 Jul 2005 16:57:08 -0400 Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> Jonathan Apostoles <jon.apostoles-1vwOZer+2/nEIuZ0ZFnJtQ@public.gmane.org> writes: > > > Item has_many :meta_tags > > MetaTag belongs_to :item > > > > How should these be named so that ActiveRecord can automatically map > > and knit together the has_many relationship? > > > > The collection that should be at item.meta_tags should come from the > > form post as MetaTag objects (which have the meta tag ID, and the > > value from the select box). > > I think Rails 0.13 now creates a item.meta_tags= method so that > item.update_attributes will work as long as you pass in a list of > meta_tag ids in the params[:item] hash. You can look at this page on > the wiki for a similar type problem: > > http://wiki.rubyonrails.com/rails/show/CheckboxHABTM > -- > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails