# Any similar to mysql\_insert\_id (PHP) in RoR

**URL:** <https://www.ruby-forum.com/t/any-similar-to-mysql-insert-id-php-in-ror/91987>\
**Category:** Rails\
**Created:** [April 14, 2007, 12:56am UTC](https://www.ruby-forum.com/t/any-similar-to-mysql-insert-id-php-in-ror/91987 "2007-04-14T00:56:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wynners33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/82dd89/32.png) [@wynners33](https://www.ruby-forum.com/u/wynners33)\
**Post date:** [April 14, 2007, 12:56am UTC](https://www.ruby-forum.com/t/any-similar-to-mysql-insert-id-php-in-ror/91987/1 "2007-04-14T00:56:28Z")

</div>

I used a PHP function mysql\_insert\_id to get the ID generated from the  
previous INSERT operation. I wonder if there is something similar that I  
can use in Ruby/Rails?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![wynners33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/82dd89/32.png) [@wynners33](https://www.ruby-forum.com/u/wynners33)\
**Post date:** [April 14, 2007, 1:12am UTC](https://www.ruby-forum.com/t/any-similar-to-mysql-insert-id-php-in-ror/91987/2 "2007-04-14T01:12:35Z")

</div>

Once you save @object, just use @object.id.

–  
Building an e-commerce site with Rails?  
[http://agilewebdevelopment.com/rails-ecommerce](http://agilewebdevelopment.com/rails-ecommerce)

Meet up at RailsConf:  
[http://railsconf2007.conferencemeetup.com/](http://railsconf2007.conferencemeetup.com/)

---

<div class="post-metadata">

**Author:** ![wynners33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/82dd89/32.png) [@wynners33](https://www.ruby-forum.com/u/wynners33)\
**Post date:** [April 14, 2007, 2:43am UTC](https://www.ruby-forum.com/t/any-similar-to-mysql-insert-id-php-in-ror/91987/3 "2007-04-14T02:43:44Z")

</div>

Benjamin C. wrote:

> Once you save @object, just use @object.id.

Thanks Benjamin. So you mean @object.id will be the auto-increment id  
created in MySql?

All I want to do is that - in one single page, allow people to sign up  
for a company and register for many users (in the same company). Once  
user clicks submit, it will first create the company (i.e. insert into  
company table and return id) and then add each user to users table  
(using the same company\_id)

---

<div class="post-metadata">

**Author:** ![wynners33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/82dd89/32.png) [@wynners33](https://www.ruby-forum.com/u/wynners33)\
**Post date:** [April 14, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/any-similar-to-mysql-insert-id-php-in-ror/91987/4 "2007-04-14T21:01:50Z")

</div>

On Apr 13, 2007, at 5:43 PM, Gordon Lo wrote:

> user clicks submit, it will first create the company (i.e. insert into  
> company table and return id) and then add each user to users table  
> (using the same company\_id)

Yes, it is the id provided by MySQL. Assuming you have the  
relationship between company and users in your models, you can also  
do something like this:

@company = Company.create(params[:company])  
params[:users].each do |user|  
@company.users.create(user)  
end

–  
Building an e-commerce site with Rails?  
[http://agilewebdevelopment.com/rails-ecommerce](http://agilewebdevelopment.com/rails-ecommerce)

Meet up at RailsConf:  
[http://railsconf2007.conferencemeetup.com/](http://railsconf2007.conferencemeetup.com/)
