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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> 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. > > HTHOk 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Lagace wrote:> Bob Showalter wrote: > 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 > > > 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 helpOh and what do i put as my id and name for my text input also? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 5/15/07, Matthew Lagace <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> On 5/15/07, Matthew Lagace <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---