I''ve got several habtm''s working fine in my app but have been having trouble with a new twist: imagine an app that tracks students and courses that they''re taking. So there''s a student Class and a course Class, and the tables also, and a students_courses table. This stuff I have down cold. The problem was I wanted a form that allowed associating a student with up to three courses, each individually selected from a drop-down select box: Student: John Course 1: (select box) Course 2: (select box) Course 3: (select box) I tried the following for my select box code in my view (this is pseudo-code, I don''t have the source with me): <% select :student, :course_id, Course.find(:all) %> The error I''m getting is with the second parameter above. I think I''ve got that wrong. Is there any way to do what I''m doing? I think the problem is that I don''t understand how to modify a collection of many courses when I''ve got three separate select boxes. Thanks very much. -- Posted via http://www.ruby-forum.com/.
If I understand you correctly, you want an admin page which gives three select boxes (preferably showing the course name) which you will use to populate your courses_students joining table upon posting. I often want to create select boxes on admin pages, so I wrote a class function on the model which returned an array of arrays - [[name1, id1], [name2, id2], ...] etc. which I called ''options''. I passed that into the select helper function... In app/models/course.rb: class Course < ActiveRecord::Base def self.options options = [] find(:all).each { |item| options += [[item.name, item.id]] } options end end and in my _form.rhtml file: <%= select ''course'', ''course_id_1'', @courses %></p> @courses comes from the controller: private def prepare_admin @courses = Genre.options end This all seems over elaborate and weighty... ut it works nicely enough. I''m guessing there''s probably a helper function I haven''t seen which automates all this in the view layer only. I could make the model function into a mixin by adding a little generic-ness... But I don''t know where to add it in as yet. Or I could add the functionality to all active records by adding it to the active record class. It all seems a bit wonky though. It ''feels'' like I''m creating display information in the model layer, which doesn''t make me happy. What''s the one-liner solution, guys? Ben
Supplementary: Once the select box is displaying (when I tried to use a .find, I just got a select box full of object serial numbers), you''ll need to: 1: Make sure the view is generating field names which aren''t the same as any in the student object. (course_id_1, course_id_2, etc.) 2: In the receiving controller, you will have to catch these by hand (params[:student_course_id_1] type of thing) and create new student_course type objects. Load them up with the values (if the course IDs are not nill), then save them. I don''t think there''s an automatic way to do this, but I''m new to all this too. As you can probably tell.