# Inserting parent\_id

**URL:** <https://www.ruby-forum.com/t/inserting-parent-id/57029>\
**Category:** Rails\
**Created:** [April 6, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/inserting-parent-id/57029 "2006-04-06T17:46:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 6, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/inserting-parent-id/57029/1 "2006-04-06T17:46:34Z")

</div>

I have two tables:

- projects
- lines

lines has a foreign key constraint to projects.id

When I’m creating a new line object, the database fails on insert with  
the following error:

“PGError: ERROR: null value in column “project\_id” violates not-null  
constraint”

My code looks like this:

def create\_lines  
@project = params[:id]  
@line = Line.new(params[:line])  
if @line.save  
flash[:notice] = ‘Lines was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

Here’s the template:

\<%= form\_tag :action =\> ‘create\_lines’, :id =\> @project %\>

\<% while @count \< @line\_qty %\>

```
<tr>
    <td><%= text_field("line", "partno", "size" => 20) %></td>
    <td><%= text_field("line", "qty", "size" => 20) %></td>
    <td><%= text_field("line", "target_price", "size" => 20) %></td>
    <td><%= text_field("line", "condition", "size" => 20) %></td>
</tr>

```

\<% @count += 1 %\>  
\<% end %\>

  
  

| \<%= submit\_tag(" ADD LINES ") %\> |

\<%= end\_form\_tag %\>

Any idea why it doesn’t pickup the parent id ?

/mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 6, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/inserting-parent-id/57029/2 "2006-04-06T18:19:56Z")

</div>

mich wrote:

> ```
> redirect_to :action => 'list'
> 
> <td colspan="4"><%= submit_tag(" ADD LINES ") %></td>
> </tr>
> 
> ```
> 
> \<%= end\_form\_tag %\>
> 
> Any idea why it doesn’t pickup the parent id ?
> 
> /mich

Either I’m missing something here, or you really don’t assign a project  
to  
the line. The params[:line] you’re using to create a new Line doesn’t  
include a project.

I don’t know how you’re interface is set up, but either include a field  
for  
the project (usually a select), or add “@line.project = @project”  
between  
the save and new statements of the line.

There is no need for the @project and @lines variables in the  
create\_lines  
method to be isntances variables BTW. You can remove the @'s.

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 6, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/inserting-parent-id/57029/3 "2006-04-06T22:07:01Z")

</div>

Wiebe C. wrote:

> Either I’m missing something here, or you really don’t assign a project  
> to  
> the line. The params[:line] you’re using to create a new Line doesn’t  
> include a project.
> 
> I don’t know how you’re interface is set up, but either include a field  
> for  
> the project (usually a select), or add “@line.project = @project”  
> between  
> the save and new statements of the line.

Beautiful - that did the trick. Pretty obvious, it seems now 😉

> There is no need for the @project and @lines variables in the  
> create\_lines  
> method to be isntances variables BTW. You can remove the @'s.

That makes sense, thanks.

However, I still seem to have issues inserting the rows. As you can see  
from my code snippets, I’d like to insert several line items…

\<% while @count \< @line\_qty %\>

```
<tr>
    <td><%= text_field("line", "partno", "size" => 20) %></td>
    <td><%= text_field("line", "qty", "size" => 20) %></td>
    <td><%= text_field("line", "target_price", "size" => 20) %></td>
    <td><%= text_field("line", "condition", "size" => 20) %></td>
</tr>

```

\<% @count += 1 %\>  
\<% end %\>

How do I tell the create\_lines action to process all the lines, and also  
how do I send multiple lines, with multiple items to the controller ?

Thanks in advance,

/mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 6, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/inserting-parent-id/57029/4 "2006-04-06T22:44:54Z")

</div>

On Thursday 06 April 2006 22:07, mich wrote:

> ```
> </tr>
> 
> ```

Hmm, that’s going to be a bit difficult to explain. I have a similair  
need in  
an app I’m making. What I did was use hashes.

In your view, you will have to make a hash, “lines” for example, with  
“line.id.to\_s” as key. You can then supply it to a text\_field\_tag like  
so:

text\_field\_tag(“lines[#{line.id}]”, rest\_of\_options)

The object “line” is a parameter of a block (is that called a parameter?  
The  
thing between |'s?) for example, when iterating over a collection of  
lines.  
This way, you can store a collection of fields in a hash, which you can  
unpack  
again in your controller.

I know this may be of little help, but as I said, it’s going to be a  
little  
difficult to explain. Also, I’d need more info on your app to give  
accurate  
help, but that would most likely mean a lot of work for me. I do need  
time to  
work on my own app, you see 🙂
