# Inserting to a table that has a foreign key

**URL:** <https://www.ruby-forum.com/t/inserting-to-a-table-that-has-a-foreign-key/103631>\
**Category:** Rails\
**Created:** [May 8, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/inserting-to-a-table-that-has-a-foreign-key/103631 "2007-05-08T17:46:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fivetimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a587f6/32.png) [@fivetimes](https://www.ruby-forum.com/u/fivetimes)\
**Post date:** [May 8, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/inserting-to-a-table-that-has-a-foreign-key/103631/1 "2007-05-08T17:46:30Z")

</div>

Hi,

I’m new to ruby on rails and I’m working on a very small project. I’m in  
the very early stages of development and I’m already stuck. Here’s the  
problem:

I have two tables:

programs

- program\_id primary key auto\_increment
- program\_name

modules

- module\_id primary key auto\_increment
- module\_code
- module\_name
- program\_id foreign key

My first steps in coding are to do a simple admin section, where the  
user can add new programs and modules. The ‘adding new programs’  
functionality works, but the ‘adding new modules’ errors with the  
following message: ‘wrong number of arguments (1 for 0)’. I’m guessing  
this has something to do with the fact that program\_id is a foreign in  
the modules table. Can anybody advise on where I’m going wrong please?

* * *

## Models:

class Program \< ActiveRecord::Base  
has\_many :modules  
end

class Module \< ActiveRecord::Base  
belongs\_to :program  
end

* * *

## add\_module View:

\<% @page\_title = “AddModule” -%\>  
\<%= start\_form\_tag(:action =\> “save\_module”) %\>

Module Code:  
\<%= text\_field(“module”, “module\_code”, “size” =\> 40 ) %\>

Module Name:  
\<%= text\_field(“module”, “module\_name”, “size” =\> 40 ) %\>

Program Name:  
\<%= collection\_select(“module”, “program\_id” , Program.find\_all,  
“program\_id”, “program\_name”) %\>

\<%= submit\_tag(" ADD ") %\>  
\<%= end\_form\_tag %\>

* * *

## Controller:

def add\_module  
end

def save\_module  
@module = Module.new(params[:module])

```
if @module.save
  flash[:notice] = "Module '" + @module.module_name + "' was

```

sucessfully added!"  
redirect\_to :action =\> ‘list’  
else  
redirect\_to :action =\> “add\_module”  
end  
end

* * *

## Development Log:

Processing AdminController#save\_module (for at 2007-05-08 16:35:06)  
[POST]  
Session ID: 61f30f5fc79afbce0720f9470ffc7766  
Parameters: {“module”=\>{“module\_code”=\>“Module X”, “program\_id”=\>“3”,  
“module\_name”=\>“New Module”}, “commit”=\>" ADD ",  
“action”=\>“save\_module”, “controller”=\>“admin”}

ArgumentError (wrong number of arguments (1 for 0)):

---

<div class="post-metadata">

**Author:** ![fivetimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a587f6/32.png) [@fivetimes](https://www.ruby-forum.com/u/fivetimes)\
**Post date:** [May 8, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/inserting-to-a-table-that-has-a-foreign-key/103631/2 "2007-05-08T18:06:28Z")

</div>

Trish Mulligan wrote:

> Processing AdminController#save\_module (for at 2007-05-08 16:35:06)  
> [POST]  
> Session ID: 61f30f5fc79afbce0720f9470ffc7766  
> Parameters: {“module”=\>{“module\_code”=\>“Module X”, “program\_id”=\>“3”,  
> “module\_name”=\>“New Module”}, “commit”=\>" ADD ",  
> “action”=\>“save\_module”, “controller”=\>“admin”}
> 
> ArgumentError (wrong number of arguments (1 for 0)):

In the browser it should tell you on which line this error is occurring,  
might help narrow it down?

---

<div class="post-metadata">

**Author:** ![fivetimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a587f6/32.png) [@fivetimes](https://www.ruby-forum.com/u/fivetimes)\
**Post date:** [May 8, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/inserting-to-a-table-that-has-a-foreign-key/103631/3 "2007-05-08T18:08:30Z")

</div>

Sorry, I should’ve said. The error occurs on the following line…

@module = Module.new(params[:module])

Thanks,

Trish.

---

<div class="post-metadata">

**Author:** ![fivetimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a587f6/32.png) [@fivetimes](https://www.ruby-forum.com/u/fivetimes)\
**Post date:** [May 8, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/inserting-to-a-table-that-has-a-foreign-key/103631/4 "2007-05-08T18:18:56Z")

</div>

Trish Mulligan wrote:

> Sorry, I should’ve said. The error occurs on the following line…
> 
> @module = Module.new(params[:module])
> 
> Thanks,
> 
> Trish.

Perhaps it is because Module is a reserved word in Ruby, or rather a  
ruby class. When you call Module.new it is trying to create a new module  
object.

[http://www.ruby-doc.org/core/classes/Module.html](http://www.ruby-doc.org/core/classes/Module.html)

Try changing the name of your model to something else.

---

<div class="post-metadata">

**Author:** ![fivetimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a587f6/32.png) [@fivetimes](https://www.ruby-forum.com/u/fivetimes)\
**Post date:** [May 8, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/inserting-to-a-table-that-has-a-foreign-key/103631/5 "2007-05-08T18:26:31Z")

</div>

Thanks for taking the time to reply.

I have changed the name of my model and it now works.
