# Rails 2.3 nested forms with has\_many through checkboxes

**URL:** <https://www.ruby-forum.com/t/rails-2-3-nested-forms-with-has-many-through-checkboxes/165351>\
**Category:** Rails\
**Created:** [April 12, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/rails-2-3-nested-forms-with-has-many-through-checkboxes/165351 "2009-04-12T23:48:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![OKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f4b2a3/32.png) [@OKR](https://www.ruby-forum.com/u/OKR)\
**Post date:** [April 12, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/rails-2-3-nested-forms-with-has-many-through-checkboxes/165351/1 "2009-04-12T23:48:05Z")

</div>

I was wondering if anyone knew of a way to combine the new nested  
forms in rails 2.3 with a has\_many through relationship via  
checkboxes. Basically I have a page model, a page\_category model and  
a category\_items join table. Pages have many page categories through  
category items. The category\_items table is polymorphic so i can use  
it for other models who need categories (maybe this is  
overcomplicating everything though?).

In a page’s edit form I want to be able to dynamically add new  
category fields and then associate the new ones I create and the other  
already created page categories to the current page via checkboxes.  
Hopefully this makes sense and can actually be done, I’d rather not  
have to go to another controller/form just to create and edit page  
categories.

class Page \< ActiveRecord::Base  
has\_many :category\_items, :as =\> :item, :dependent =\> :destroy  
has\_many :page\_categories, :through =\> :category\_items, :source  
=\> :category, :source\_type =\> “PageCategory”, :uniq =\> true  
accepts\_nested\_attributes\_for :page\_categories, :allow\_destroy =\>  
true, :reject\_if =\> proc { |attrs| attrs.all? { |k, v| v.blank? } }  
end

class PageCategory \< ActiveRecord::Base  
has\_many :pages, :through =\> :category\_items, :source  
=\> :item, :source\_type =\> ‘Page’, :uniq =\> true  
end

class CategoryItem \< ActiveRecord::Base  
belongs\_to :category, :polymorphic =\> true  
belongs\_to :item, :polymorphic =\> true  
end

Page Category partial for my page form (hooks into a jquery script to  
dynamically add/remove page category form fields)…

Categories\<%=  
hidden\_field\_tag “page[page\_category\_ids][]”, “” %\>

 \<%- for category in PageCategory.all -%\> - \<%= check\_box\_tag "page[page\_category\_ids][]", category.id, @page.page\_categories.include?(category), :class =\> "checkbox" %\>&nbsp;\<% f.fields\_for category do |category\_form| %\>\<%= category\_form.text\_field :title, :style =\> "width: 200px;", :class =\> "textfield small formElement" %\>\<% end %\> 
 \<%- end -%\>

Right now, if I try and create or update a page I get the following  
error…

ActiveRecord::UnknownAttributeError in Admin/pagesController#update

unknown attribute: page\_category  
RAILS\_ROOT: /Users/rajo/Sites/boilerplate

Application Trace | Framework Trace | Full Trace  
/Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/  
active\_record/base.rb:2745:in `attributes=' /Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/ active_record/base.rb:2741:in`each’  
/Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/  
active\_record/base.rb:2741:in `attributes=' /Users/rajo/Sites/boilerplate/vendor/rails/activerecord/lib/ active_record/base.rb:2627:in`update\_attributes’  
/Users/rajo/Sites/boilerplate/app/controllers/admin/  
pages\_controller.rb:107:in `update' /Users/rajo/Sites/boilerplate/app/controllers/admin/ pages_controller.rb:106:in`update’  
Request

Parameters:

{“commit”=\>“Save”,  
“\_method”=\>“put”,  
“authenticity\_token”=\>“DvvJPsU9c942x1Ak5wOIm8V4wYv8AClftKJ7ohGqOo0=”,  
“id”=\>“about”,  
“page”=\>{“name”=\>“About”,  
“title”=\>“About Us”,  
“body”=\>“

About this site.

”,  
“published”=\>“1”,  
“page\_category”=\>{“title”=\>“Default Two”},  
“page\_category\_ids”=\>["",  
“1”,  
“2”],  
“home”=\>“0”}}  
Show session dump

Response

Headers:

{“Content-Type”=\>"",  
“Cache-Control”=\>“no-cache”}

Thanks for any help in advance.
