I have modified a basic function for creating a new record. def create @contact = Contact.new(params[:contact]) @contact.user_id = current_user.id @contact.save end I also have another table that holds records as to which users can access which contacts. Essentially, it creates a row that has the user_id and contact_id. Since the id for the contact is automatically assigned as a primary key, is there any way I can access it directly after the contact is saved - or do I have to do a find to try to track down the record that was just created? -- 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Jul-09 19:58 UTC
Re: Is there a quicker way to find info of a new record?
On Jul 9, 2007, at 1:46 PM, Robert Scott wrote:> > I have modified a basic function for creating a new record. > > def create > @contact = Contact.new(params[:contact]) > @contact.user_id = current_user.id > @contact.save > end > > I also have another table that holds records as to which users can > access which contacts. Essentially, it creates a row that has the > user_id and contact_id. > > Since the id for the contact is automatically assigned as a primary > key, > is there any way I can access it directly after the contact is saved - > or do I have to do a find to try to track down the record that was > just > created?This was just discussed on the list: After the .save, @contact.id is the primary key of the newly saved record. However, did you know that you can do: @contact = current_user.create_contact(params[:contact]) See the rdoc for belongs_to -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---