Multiple checkboxes for a has_many through not saving

*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| %>

<%= check_box_tag
“language_background_and_interest[book_ids][]”, book.id %>
<%= book.name.humanize %>
<% end %>

<% 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| %>

<%= check_box_tag
“language_background_and_interest_attributes[book_ids][]”, book.id %>
<%= book.name.humanize %>
<% end %>

<% end %>

On Dec 26, 12:15am, why-el [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs