Forum: Ruby on Rails Is there a quicker way to find info of a new record?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert S. (Guest)
on 2007-07-09 21:46
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?
Rob B. (Guest)
on 2007-07-09 23:59
(Received via mailing list)
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
removed_email_address@domain.invalid
This topic is locked and can not be replied to.