*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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
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.


Reply via email to