About a simple insert

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?

On 5/15/07, Matthew L. [email protected] 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

Bob S. wrote:

On 5/15/07, Matthew L. [email protected] 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

Ok i got that, but in my new.rhtml, what do I put in the value field of
my text input? I tried <%= @client.details.name %> but it doesn’t work.

Thanks alot for the help

Matthew L. wrote:

Bob S. wrote:
On 5/15/07, Matthew L. [email protected] 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

Ok i got that, but in my new.rhtml, what do I put in the value field of
my text input? I tried <%= @client.details.name %> but it doesn’t work.

Thanks alot for the help

Oh and what do i put as my id and name for my text input also?

Thanks

On 5/15/07, Matthew L. [email protected] wrote:

Ok i got that, but in my new.rhtml, what do I put in the value field of
my text input? I tried <%= @client.details.name %> but it doesn’t work.

Oh, I see what you’re saying.

You should keep two separate objects for the purpose of the form:

(in controller)

@client = Client.new
@detail = Detail.new

Then in the view:

<%= text_field :detail, :name %>

Before the save, you would add the detail to the client:

@client.details << @detail
@client.save

Bob S. wrote:

On 5/15/07, Matthew L. [email protected] wrote:
Ok i got that, but in my new.rhtml, what do I put in the value field of
my text input? I tried <%= @client.details.name %> but it doesn’t work.

Oh, I see what you’re saying.

You should keep two separate objects for the purpose of the form:

Wow! Thank you very much! I really needed to know this and i’m glad I
came here. Thanks alot for your help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs