# Is there a quicker way to find info of a new record?

**URL:** <https://www.ruby-forum.com/t/is-there-a-quicker-way-to-find-info-of-a-new-record/108942>\
**Category:** Rails\
**Created:** [July 9, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/is-there-a-quicker-way-to-find-info-of-a-new-record/108942 "2007-07-09T19:46:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [July 9, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/is-there-a-quicker-way-to-find-info-of-a-new-record/108942/1 "2007-07-09T19:46:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [July 9, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/is-there-a-quicker-way-to-find-info-of-a-new-record/108942/2 "2007-07-09T21:59:48Z")

</div>

On Jul 9, 2007, at 1:46 PM, Robert S. wrote:

> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
