# Newbie - HABTM problems writing to Join Table in SQL

**URL:** <https://www.ruby-forum.com/t/newbie-habtm-problems-writing-to-join-table-in-sql/93600>\
**Category:** Rails\
**Created:** [April 5, 2006, 12:55am UTC](https://www.ruby-forum.com/t/newbie-habtm-problems-writing-to-join-table-in-sql/93600 "2006-04-05T00:55:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [April 5, 2006, 12:55am UTC](https://www.ruby-forum.com/t/newbie-habtm-problems-writing-to-join-table-in-sql/93600/1 "2006-04-05T00:55:46Z")

</div>

I am attempting to document a join between two tables. There seems to be  
no writing to my database. Please help me out it is driving me absolutly  
crazy.

Basically i’ve got a table of directors that is called when the new/edit  
controller model for films is activated. It shows the user a selectable  
list of directors they can apply to films. The problem is in the  
update/create part. I cannot figure out what to write to make it update  
my directors\_films database table.  
  
Thanks so much for whomever braves this problem… I promise to return  
the favor to another seeking knowledge someday!

-----schema.rb-----

ActiveRecord::Schema.define() do

create\_table “directors”, :force =\> true do |t|  
t.column “name”, :string, :default =\> “na”, :null =\> false  
t.column “biography”, :text, :default =\> “”, :null =\> false  
end

create\_table “directors\_films”, :id =\> false, :force =\> true do |t|  
t.column “director\_id”, :integer, :default =\> 0, :null =\> false  
t.column “film\_id”, :integer, :default =\> 0, :null =\> false  
end

create\_table “films”, :force =\> true do |t|  
t.column “title”, :string, :default =\> “”, :null =\> false  
t.column “release\_date”, :date, :null =\> false  
end

end

------director.rb-----

class Director \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :films  
end

------film.rb-------

class Film \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :directors  
end

--------application\_helper.rb---------

module ApplicationHelper

def director\_dropdown  
select(:director, :id, Director.find(:all, :order =\> “name”).map {|d|  
[d.name, d.director\_id] })  
end  
end

--------films/form.rhtml-----------

Title  
\<%= text\_field 'film', 'title' %\>  
Director  
\<%= director\_dropdown %\>

Release date  
\<%= date\_select 'film', 'release\_date', :order =\> [:day, :month, :year], :start\_year =\> 1900 %\>

--------films\_controller----------

class FilmsController \< ApplicationController  
def index  
list  
render :action =\> ‘list’  
end

# GETs should be safe (see

[URIs, Addressability, and the use of HTTP GET and POST](http://www.w3.org/2001/tag/doc/whenToUseGet.html))  
verify :method =\> :post, :only =\> [:destroy, :create, :update],  
:redirect\_to =\> { :action =\> :list }

def list  
@film\_pages, @films = paginate :films, :per\_page =\> 10  
end

def show  
@film = Film.find(params[:id])  
end

def new  
@film = Film.new  
@directors = Director.find\_all  
end

def create  
@film = Film.new(params[:film])  
if @film.save  
flash[:notice] = ‘Film was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def edit  
@film = Film.find(params[:id])  
@directors = Director.find\_all  
end

def update  
@film = Film.find(@params[:id])  
if @film.update\_attributes(params[:film])  
flash[:notice] = ‘Film was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @film  
else  
render :action =\> ‘edit’  
end  
end

def destroy  
Film.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end  
end

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [April 5, 2006, 6:59am UTC](https://www.ruby-forum.com/t/newbie-habtm-problems-writing-to-join-table-in-sql/93600/2 "2006-04-05T06:59:25Z")

</div>

> def create  
> @film = Film.new(params[:film])

After this line you will (I think) want:

@film.directors = Director.find(params[:id])

you might need:

@film.directors = Director.find(params[:director][:id])

but I don’t think so.

> ```
> if @film.save
> flash[:notice] = 'Film was successfully created.'
> redirect_to :action => 'list'
> else
> render :action => 'new'
> end
> 
> ```
> 
> end

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [April 5, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/newbie-habtm-problems-writing-to-join-table-in-sql/93600/3 "2006-04-05T15:14:44Z")

</div>

I have tried similar to this, but have had no successes. What I get in  
response is this:

* * *

ActiveRecord::RecordNotFound in Films#create

Couldn’t find Director without an ID  
RAILS\_ROOT: script/…/config/…

Application Trace | Framework Trace | Full Trace  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active\_record/base.rb:939:in  
`find_from_ids' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/base.rb:382:in`find’  
#{RAILS\_ROOT}/app/controllers/films\_controller.rb:26:in `create’  
Request

Parameters: {“director”=\>{“id”=\>“1”}, “commit”=\>“Create”,  
“film”=\>{“image\_url”=\>“na”, “crew”=\>"", “title”=\>"", “commentary”=\>"",  
“film\_url”=\>“na”, “release\_date(1i)”=\>“2006”, “video\_url”=\>“na”,  
“release\_date(2i)”=\>“4”, “release\_date(3i)”=\>“5”, “distributor”=\>“na”,  
“cast”=\>"", “synopsis”=\>""}}

Show session dump

Response

Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

* * *

Could you explain what this means?

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [April 5, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/newbie-habtm-problems-writing-to-join-table-in-sql/93600/4 "2006-04-05T18:25:55Z")

</div>

> ActiveRecord::RecordNotFound in Films#create
> 
> Couldn’t find Director without an ID  
> RAILS\_ROOT: script/…/config/…

> Parameters: {“director”=\>{“id”=\>“1”}, “commit”=\>“Create”,  
> “film”=\>{“image\_url”=\>“na”, “crew”=\>"", “title”=\>"", “commentary”=\>"",  
> “film\_url”=\>“na”, “release\_date(1i)”=\>“2006”, “video\_url”=\>“na”,  
> “release\_date(2i)”=\>“4”, “release\_date(3i)”=\>“5”, “distributor”=\>“na”,  
> “cast”=\>"", “synopsis”=\>""}}
> 
> Show session dump
> 
> Response
> 
> Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}
> 
> * * *
> 
> Could you explain what this means?

The error, which I assume is on this line:

@film.directors = Director.find(params[:director][:id])

means that it’s trying to run find without having an :id parameter.

Which is odd because as you see from this:

> Parameters: {“director”=\>{“id”=\>“1”}, “commit”=\>“Create”,  
> “film”=\>{“image\_url”=\>“na”, “crew”=\>"", “title”=\>"", “commentary”=\>"",  
> “film\_url”=\>“na”, “release\_date(1i)”=\>“2006”, “video\_url”=\>“na”,  
> “release\_date(2i)”=\>“4”, “release\_date(3i)”=\>“5”, “distributor”=\>“na”,  
> “cast”=\>"", “synopsis”=\>""}}

Specifically, the part

“director”=\>{“id”=\>“1”}

Indicates that the id should be accessible by [:director][:id]. I think.  
😛

You might try it again to make sure there were no small syntax mistakes.  
In the mean time, anyone know what I’m missing? 🙂

-Adam
