# @@identity

**URL:** <https://www.ruby-forum.com/t/identity/122166>\
**Category:** Ruby\
**Created:** [December 5, 2007, 3:43am UTC](https://www.ruby-forum.com/t/identity/122166 "2007-12-05T03:43:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![four\_mrperfect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@four\_mrperfect](https://www.ruby-forum.com/u/four_mrperfect)\
**Post date:** [December 5, 2007, 3:43am UTC](https://www.ruby-forum.com/t/identity/122166/1 "2007-12-05T03:43:26Z")

</div>

I can’t seem to find any resources which tell me how to do the  
following. When I write some content to a table in my database (mysql),  
I want to retrieve the id of the newly inserted record; an equivalent of  
select @@IDENTITY from sometable. How can I do this in ruby?

In my code, I have a hash:

params[:customer] = row.to\_hash  
@customer = @asset.customers.build(params[:customer])

# now I want to store, in table x, the id of this record which was just

inserted into the customers table.

---

<div class="post-metadata">

**Author:** ![four\_mrperfect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@four\_mrperfect](https://www.ruby-forum.com/u/four_mrperfect)\
**Post date:** [December 5, 2007, 5:48am UTC](https://www.ruby-forum.com/t/identity/122166/2 "2007-12-05T05:48:54Z")

</div>

doesn’t anyone know how to retrieve the id of the last inserted record,  
using ruby?

---

<div class="post-metadata">

**Author:** ![four\_mrperfect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@four\_mrperfect](https://www.ruby-forum.com/u/four_mrperfect)\
**Post date:** [December 5, 2007, 6:35am UTC](https://www.ruby-forum.com/t/identity/122166/3 "2007-12-05T06:35:58Z")

</div>

For Mrperfect wrote:

> doesn’t anyone know how to retrieve the id of the last inserted record,  
> using ruby?

It’s not that, it’s that no-one tends to respond quickly to folk who  
don’t  
bother to use Google. Look at the following link, and show the source of  
“insert”:

[http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SQLServerAdapter.html#M000779](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SQLServerAdapter.html#M000779)

To learn more, you might need to check out the ActiveRecord source code  
using svn.

---

<div class="post-metadata">

**Author:** ![four\_mrperfect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@four\_mrperfect](https://www.ruby-forum.com/u/four_mrperfect)\
**Post date:** [December 5, 2007, 8:20am UTC](https://www.ruby-forum.com/t/identity/122166/4 "2007-12-05T08:20:18Z")

</div>

First off, thanks for your reply. If it’s the case the people haven’t  
responded to my question for thinking that I haven’t googled, I think  
think they’re a bunch of stuck-ups!!! I did search but hadn’t found my  
answer. I even went through the trouble of signing up for an account,  
on here, just to ask this question!! I’m new to ruby.

I installed the activesupport gem but now I’m getting the error:

undefined method `execute’

I get the impression that this adapter is for ms sql server. would it  
work with mysql as well?

Clifford H. wrote:

> For Mrperfect wrote:  
> It’s not that, it’s that no-one tends to respond quickly to folk who  
> don’t  
> bother to use Google. Look at the following link, and show the source of  
> “insert”:

---

<div class="post-metadata">

**Author:** ![four\_mrperfect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@four\_mrperfect](https://www.ruby-forum.com/u/four_mrperfect)\
**Post date:** [December 5, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/identity/122166/5 "2007-12-05T15:03:35Z")

</div>

Alex Y. wrote:

> You want a:
> 
> customer = @asset.customers.create(params[:customer])
> 
> following which, customer.id will have the ID of the customer record as  
> inserted into the customers table.
> 
> –  
> Alex

GREAT! Thanks, Alex!

---

<div class="post-metadata">

**Author:** ![four\_mrperfect](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@four\_mrperfect](https://www.ruby-forum.com/u/four_mrperfect)\
**Post date:** [December 5, 2007, 9:52am UTC](https://www.ruby-forum.com/t/identity/122166/6 "2007-12-05T09:52:32Z")

</div>

For Mrperfect wrote:

> First off, thanks for your reply. If it’s the case the people haven’t  
> responded to my question for thinking that I haven’t googled, I think  
> think they’re a bunch of stuck-ups!!!  
> It’s also worth mentioning that your initial request and follow-up were  
> within two hours of each other, in the middle of the night (for me).  
> It’s somewhat unreasonable to expect an answer in that timeframe,  
> especially when the answer to your problem is actually in the official  
> docs.

> I did search but hadn’t found my  
> answer.  
> It’s worth mentioning that, because the assumption otherwise will be  
> that you haven’t.

> I even went through the trouble of signing up for an account,  
> on here, just to ask this question!! I’m new to ruby.
> 
> I installed the activesupport gem but now I’m getting the error:
> 
> undefined method `execute’
> 
> I get the impression that this adapter is for ms sql server. would it  
> work with mysql as well?  
> If you’re using ActiveRecord (not sure why you need activesupport for  
> this), then it’s taken care of automatically. In any case, your problem  
> isn’t getting the last inserted ID, because this code:

customer = @asset.customers.build(params[:customer])

_doesn’t_ save the customer, according to the docs here:  
[http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000658](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000658).

You want a:

customer = @asset.customers.create(params[:customer])

following which, customer.id will have the ID of the customer record as  
inserted into the customers table.

–  
Alex
