# Creating a contact form that will update 2 tables in database

**URL:** <https://www.ruby-forum.com/t/creating-a-contact-form-that-will-update-2-tables-in-database/233272>\
**Category:** Rails\
**Created:** [November 30, 2014, 12:15am UTC](https://www.ruby-forum.com/t/creating-a-contact-form-that-will-update-2-tables-in-database/233272 "2014-11-30T00:15:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [November 30, 2014, 12:15am UTC](https://www.ruby-forum.com/t/creating-a-contact-form-that-will-update-2-tables-in-database/233272/1 "2014-11-30T00:15:23Z")

</div>

Here is my stackoverflow question I posted

> <https://stackoverflow.com/questions/27208065/creating-a-contact-form-that-updates-2-tables>

Here is the question if you do not want to visit the website

I am trying to create a form that updates 2 tables in my database. I’m  
not  
quite sure how to build this contact form quite yet. So my first short  
term  
goal is to simply update my `contact_requests` and `users` tables. I am  
aiming to do a `one to many` relationship between `users` and  
`contact_requests`. This allows one `user` to have many  
`contact_requests`.  
I am new to rails so I feel like there are gaps in my code and I am  
making  
some mistakes as well.

I currently have a `user` table and a `contact_request` table:

![enter image description here](http://i.stack.imgur.com/HE7KZ.png)

`user_id` from the `contact_requests` table is a foreign key that  
references `users.id`

Here is by current schema code:

```
ActiveRecord::Schema.define(version: 20141127114323) do
  create_table "contact_requests", force: true do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "message", null: false
    t.integer "user_id"
  end

  add_index "contact_requests", ["user_id"], name:

```

“index\_contact\_requests\_on\_user\_id”, using: :btree

```
  create_table "users", force: true do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "first_name", null: false
    t.string "last_name", null: false
    t.string "email", null: false
    t.string "phone_number"
  end
end

```

Here are my models and controllers:

```
class User < ActiveRecord::Base
  has_many :contact_requests
  # validate the presence of the attributes
  validates(:first_name, presence: true)
  validates(:last_name, presence: true)
  validates(:email, presence: true)
end

class ContactRequest < ActiveRecord::Base
  belongs_to :user
  validates :user_id, presence: true
  validates :message, presence: true, length: { maximum: 500 }
end

class ContactController < ApplicationController
  def new
  @user = User.new
  @contact_request = ContactRequest.new
  end
end

```

I am getting this error:

![enter image description here](http://i.stack.imgur.com/hmMT5.png)

form:

```
<%= form_for(:contact, remote: true, class: "contact-input") do |f| 

```

%\>

  

  

  
\<%= f.text\_field(:user, :first\_name, class:  
“form-control”,  
placeholder: “First name”)%\>  

  

  
\<%= f.text\_field(:user, :last\_name, class:  
“form-control”,  
placeholder: “Last name”) %\>  

  

  
\<%= f.email\_field(:user, :email, class: “form-control”,  
placeholder: “Email”) %\>  

  

  
\<%= f.telephone\_field(:user, :phone\_number, class:  
“form-control”, placeholder: “Phone number”) %\>  

  

  

  

  
\<%= f.text\_area(:contact\_request, :message, class:  
“form-control contact-margin”, rows: “8”, placeholder: “Message…”) %\>  

  

  

  
\<%= f.submit(class: “btn btn-xl”) %\>  
\<% end %\>
