Re: About a simple insert

Bob Showalter
Tue, 15 May 2007 13:08:39 -0000

On 5/15/07, Matthew Lagace
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:
>
> Ok, my problem is that I have a form with a client''s information.
Now in
> the form, I ask the client for his info, which inserts into the table
> "clients", and then I have another text input where he has to
provide a
> specific detail that i want to be inserted into the table
"details". How
> can I insert this data into 2 different tables?

It sounds like you should establish an association between the two models:

  class Client < ActiveRecord::Base
     has_many :details
  end

  class Detail < ActiveRecord::Base
    belongs_to :client
  end

This would require a client_id column in the details table.

Then, when the form is submitted, you would do something like:

  client = Client.new(params[:client])
  client.details.build(params[:detail])
  client.save

The one save inserts both the clients and details rows.

HTH