@@IDENTITY

F

For Mrperfect

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.
 
F

For Mrperfect

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

For Mrperfect

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?
 
A

Alex Young

For said:
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.


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.
 
F

For Mrperfect

Alex said:
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.

GREAT! Thanks, Alex!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top