All, I''m populating a view with checkboxes in a list. I want to be able to assign to my has_many relationship using the post values of these checkboxes. Essentially, I want to populate the relationship table which will create the correct join values in my model. Here is my check_box_tag code: <%= check_box_tag("@current_job[target_list_ids][]", tlist.DataSetID, @current_job.target_lists.include?(tlist)) %> So, in this example, tlist is an element of another array that I''m starting with to give me the list of choices. But what I want is to assign these choices into the "target_lists" collection of my @current_job object (again, via the "through" table based on my has_many definition. current_job :has_many target_lists :through an association. I first followed the examples of using checkboxes with HABTM. However, since I switched my model to user has_many :through, and so I''m pretty sure that I no longer get the target_lists_ids method above like I would if I were using HABTM. And actually, I''m not sure I care about that, since I know I should just be able to process through the values that I get from the form anyway. I''m having trouble getting at the values of the checkbox, even though in my parameter list, I see that they are available: "@current_job"=>{"target_list_ids"=>["1", "3"]} There''s something very basic that I''m missing here. I want to be able to grab these values in my controller method and do something like: params[@current_job][:target_list_ids].each { //Do what''s necessary to link up my models via the has_many :though relationship by populating the "through" table. } Is my problem that I''m naming my checkboxes using @current_job[target_list_ids], and this is being literally interpreted and ending up in the view that way? I hope this makes sense. I think I am generally confused about how the check_box and check_box_tag helpers work. Thanks, Wes -- Posted via http://www.ruby-forum.com/.
I have the same problem does anyone else have any suggestions on this? Wes Gamble wrote:> All, > > I''m populating a view with checkboxes in a list. I want to be able to > assign to my has_many relationship using the post values of these > checkboxes. Essentially, I want to populate the relationship table > which will create the correct join values in my model. >..snip..> I hope this makes sense. I think I am generally confused about how the > check_box and check_box_tag helpers work. > > Thanks, > Wes-- Posted via http://www.ruby-forum.com/.
JoeWooten wrote:> I have the same problem does anyone else have any suggestions on this? > > Wes Gamble wrote: >> All, >> >> I''m populating a view with checkboxes in a list. I want to be able to >> assign to my has_many relationship using the post values of these >> checkboxes. Essentially, I want to populate the relationship table >> which will create the correct join values in my model. >> > > ..snip.. > >> I hope this makes sense. I think I am generally confused about how the >> check_box and check_box_tag helpers work. >> >> Thanks, >> WesJoe, I got this working but don''t have time to post the code right now. Send an email to weyus at att dot net and please remind me to post my solution. Thanks, Wes -- Posted via http://www.ruby-forum.com/.
P.V.Anthony
2006-Jul-16 00:35 UTC
[Rails] Re: checkboxes with a has_many :through relation
On this day, 16-July-2006 4:48 AM, JoeWooten wrote:> I have the same problem does anyone else have any suggestions on this? > > Wes Gamble wrote: >> All, >> >> I''m populating a view with checkboxes in a list. I want to be able to >> assign to my has_many relationship using the post values of these >> checkboxes. Essentially, I want to populate the relationship table >> which will create the correct join values in my model. >> > > ..snip.. > >> I hope this makes sense. I think I am generally confused about how the >> check_box and check_box_tag helpers work. >> >> Thanks, >> Wes >Found these links. http://jrhicks.net/96 --> checkout the pdf http://wiki.rubyonrails.com/rails/pages/CheckboxHABTM http://wiki.rubyonrails.com/rails/pages/has_and_belongs_to_many ---> the main page P.V.Anthony
Joe, Here''s what I ended up doing... Job and TargetList are related through their association object. Basically, in the form, I''m manipulating indices of an array that I then use to populate the "through" object. So I''m not attempting to manipulate the "child" or "parent" model directly. Hope this helps, Wes Job model: has_many :job_list_associations, :foreign_key => :JobReferenceNumber has_many :target_lists, :through => :job_list_associations, :source => :target_list TargetList model: has_many :job_list_associations, :foreign_key => :DataSetID has_many :jobs, :through => :job_list_associations, :source => :job Job model: public def target_list_ids ids = Array.new unless target_lists.nil? target_lists.each do |list| ids << list.DataSetID end end ids end public def target_list_ids=(list_array) list_array.each do |id| target_lists << TargetList.find(id) end end View: <TD class="list-data"> <%= check_box_tag("current_job[target_list_ids][]", tlist.DataSetID, @current_job.target_lists.include?(tlist), :class => ''list-data'') %> <%= tlist.Alias %> </TD> Controller calls this method in the job model to manage the associations: public def set_list_relationships(selected_lists) #Find all of the existing associations before we save anything current_job_lists = Array.new JobListAssociation.find_all_by_JobReferenceNumber(self.JobReferenceNumber, :select => ''DataSetID'').each { |jla| current_job_lists << jla.DataSetID } #For each submitted list identifier... selected_lists.each { |value| #...add the association if it doesn''t exist and record that it was submitted unless current_job_lists.include?(value) jla = JobListAssociation.new jla.JobReferenceNumber = self.JobReferenceNumber jla.DataSetID = value jla.FaxDataTable = TargetList.find(value).Alias jla.save! end } #Finally, remove all of the lists that were already stored but were not selected this time around. (current_job_lists - selected_lists).each { |value| JobListAssociation.destroy_all("JobReferenceNumber = #{self.JobReferenceNumber} AND DataSetId = #{value}") } end -- Posted via http://www.ruby-forum.com/.
Seemingly Similar Threads
- has_many :through and foreign key parameters
- Give form elements "fieldWithErrors" class in non-std. way?
- belongs_to <parent name>, :foreign_key modifier not working
- validate method doesn''t recognize another instance method
- validate method not getting called?