# Create and edit with has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/create-and-edit-with-has-and-belongs-to-many/77071>\
**Category:** Rails\
**Created:** [November 23, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/create-and-edit-with-has-and-belongs-to-many/77071 "2006-11-23T23:51:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [November 23, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/create-and-edit-with-has-and-belongs-to-many/77071/1 "2006-11-23T23:51:48Z")

</div>

Hi you all,

I have the common model of “pages” and “subjects”, where a “page” can  
have many “subjects” and a “subject” can be related with many “pages”,  
so I set the “has\_and\_belongs\_to\_many” method in both models, that’s  
all.

In SQL, abbreviated, I have:  
CREATE TABLE pages (  
id int(10) unsigned  
…  
CREATE TABLE `subjects` (  
`id` smallint(5) unsigned NOT NULL auto\_increment,  
`subject` varchar(20) NOT NULL,  
…  
CREATE TABLE `pages_subjects` (  
page\_id int not null,  
subject\_id int not null,

In the views (\_form.rhtml), when creating or editing a page, I have the  
following text box:  
Subjects  
  
\<%= text\_field ‘page’, ‘subjects’ %\>

Where I will include several subjects but, just to try, now I just write  
one, (the only one I know is on the data base), and the following error  
appears when accept:  
ActiveRecord::AssociationTypeMismatch in WikiController#create  
Subject expected, got String

So, what am I doing wrong, or not doing? Should I specify more things in  
the controller when creating or editing this, or in the view?

Thank you a lot.

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [November 24, 2006, 6:46am UTC](https://www.ruby-forum.com/t/create-and-edit-with-has-and-belongs-to-many/77071/2 "2006-11-24T06:46:55Z")

</div>

What is your controller code that processes the form and the response  
from the text field?

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [November 24, 2006, 10:11am UTC](https://www.ruby-forum.com/t/create-and-edit-with-has-and-belongs-to-many/77071/3 "2006-11-24T10:11:11Z")

</div>

Damaris F. wrote:

> subject\_id int not null,  
> Okay, so here we have a field called ‘subject’ in the ‘subjects’  
> table…

> In the views (\_form.rhtml), when creating or editing a page, I have the  
> following text box:  
> Subjects  
>   
> \<%= text\_field ‘page’, ‘subjects’ %\>

And what you have asked for here is a text field for the page object.  
The value of which is to be saved into page.subjects. If you look at  
the HTML source rails will render you will see the field is called  
“page[subjects]”. If you simply go page.create(params[:page]) in your  
controller it is going to try and place the value of that text field  
(which will always be a string) into page.subjects which rails expects  
to be a collection of Subject objects. The string value actually needs  
to be added to a new Subject object, and that object then added to the  
page.subjects collection. So maybe something like:

\<%= text\_field\_tag “subject”, “subject” %\> or \<%= text\_field\_tag  
“subject[subject]” %\>

and then in the controller:

@subject.create(params[:subject])  
@page.create(params[:page])  
@page.subjects \<\< @subject

I’ll leave you to work out a more elegant solution to preventing  
duplicate subjects from being created, but hopefully that explains what  
is happening for you.

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [November 24, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/create-and-edit-with-has-and-belongs-to-many/77071/4 "2006-11-24T13:37:35Z")

</div>

> \<%= text\_field\_tag “subject”, “subject” %\> or \<%= text\_field\_tag  
> “subject[subject]” %\>
> 
> and then in the controller:
> 
> @subject.create(params[:subject])  
> @page.create(params[:page])  
> @page.subjects \<\< @subject

With the recommendation aobve, I wrote this code  
def create  
@page = Page.new\_page(@params[:id])  
if @request.post? and and @page.save  
@subject = Subject.new(params[:subject])  
@subject.save  
@page.subjects \<\< @subject  
flash[:notice] = ‘Page was successfully created.’  
redirect\_to :action =\> ‘show’, :id =\> @page.title  
end  
end  
And it worked…THANKS!

However, I would like to reuse somehow the “create” method I have in the  
SubjectsController:  
def create  
@subject = Subject.new(params[:subject])  
if @subject.save  
flash[:notice] = ‘Subject was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

So as not to write “subject.new…”, “subject.save…” in the “create” of  
the PagesController (there I could do the validations and all the  
stuff).

Any ideas? (I’m still a new new newbie ☹ )
