*I Copied this from StackOverflow since I did not get any response there. There is an update at the end. * I am not sure why my checkboxes for a has_many through are not saving. This is a rather complex form (An online application for a language summer program) so in addition to regular applicant information, I am collecting attributes of a LanguageBackgroundAndInterest model using a fields_for. Part of these attributes are the books that the applicant used to learn the language. The code looks like this: <%= f.fields_for :language_background_and_interest do |builder| %> <div class="field"> <%= hidden_field_tag "language_background_and_interest[book_ids][]" %> <% Book.all.each do |book| %> <br><%= check_box_tag "language_background_and_interest[book_ids][]", book.id %> <%= book.name.humanize %> <% end %> </div> <% end %> Both the language_background_and_interest and books are joined together using a has_many_through like so: class LanguageBackgroundAndInterest < ActiveRecord::Base attr_accessible :book_ids has_many :language_background_books has_many :books, through: :language_background_booksend class Book < ActiveRecord::Base attr_accessible :name, :publisher has_many :language_background_books has_many :language_background_and_interests, through: :language_background_booksend # Join Tableclass LanguageBackgroundBook < ActiveRecord::Base attr_accessible :language_background_and_interest_id, :book_id belongs_to :language_background_and_interest belongs_to :bookend I am not sure why the books from the checkboxes don''t get assigned to the appropriate background model. Any ideas? PS: Granted, this design is rather ambiguous (Why not make books belong to an applicant?), but I currently want to put up a prototype and I am also constrained by a dubious requirement. So I need this working. Update: I appended the word "attributes" bellow to the language_background model in the hope that the books_ids array gets inserted properly into the params hash with the other attributes. Inspecting the development.log, I can see the key language_background_and_interest_attributes twice: One for all the other data which get successfully saved, and one for the book_ids array which apparently gets ignored. Any ideas? <%= f.fields_for :language_background_and_interest do |builder| %> <div class="field"> <%= hidden_field_tag "language_background_and_interest_attributes[book_ids][]" %> <% Book.all.each do |book| %> <br><%= check_box_tag "language_background_and_interest_attributes[book_ids][]", book.id %> <%= book.name.humanize %> <% end %> </div> <% end %> -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Dp3C8JMMlN4J. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Dec-26 13:23 UTC
Re: Multiple checkboxes for a has_many through not saving
On Dec 26, 12:15 am, why-el <wael.khobala...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I appended the word "attributes" bellow to the language_background model in > the hope that the books_ids array gets inserted properly into the params > hash with the other attributes. Inspecting the development.log, I can see > the key language_background_and_interest_attributes twice: One for all the > other data which get successfully saved, and one for the book_ids array > which apparently gets ignored. Any ideas? >What does that log look like? You''d probably need to nest it in the hash for the parent attributes ie some_model[language_background_and_interest_attributes][some_id] [book_ids] The some_id for a saved record would be just its id. If you''re adding fields on the fly, then you''ll have to have the matching id - check what other parameters get submitted Fred> <%= f.fields_for :language_background_and_interest do |builder| %> > <div class="field"> > <%= hidden_field_tag "language_background_and_interest_attributes[book_ids][]" %> > <% Book.all.each do |book| %> > <br><%= check_box_tag "language_background_and_interest_attributes[book_ids][]", book.id %> > <%= book.name.humanize %> > <% end %> > </div> > <% end %>-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.