After I save a record in table A I want to save the id of that record in table B Immediatly

27 views
Skip to first unread message

honey ruby

unread,
Sep 5, 2013, 11:47:48 AM9/5/13
to rubyonra...@googlegroups.com
Hi all

I have two tables A and B
I have saved a record in table A and I want to save the id of that record in table B. How Can I do that.
Well I can do by search of same params which I save in Table A but I feel it is not that good approach.
If the app is has multiple users than that is not good way can any one help to do it better.



Thanks in Advance

Scott Ribe

unread,
Sep 5, 2013, 11:50:50 AM9/5/13
to rubyonra...@googlegroups.com
On Sep 5, 2013, at 9:47 AM, honey ruby <emailtoh...@gmail.com> wrote:

> Hi all
>
> I have two tables A and B
> I have saved a record in table A and I want to save the id of that record in table B. How Can I do that.
> Well I can do by search of same params which I save in Table A but I feel it is not that good approach.

Just save it, then use the id ;-)

Seriously, this is a very common idiom, and you'll find that rails takes care of it.



--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Walter Lee Davis

unread,
Sep 5, 2013, 11:55:17 AM9/5/13
to rubyonra...@googlegroups.com

On Sep 5, 2013, at 11:50 AM, Scott Ribe wrote:

> On Sep 5, 2013, at 9:47 AM, honey ruby <emailtoh...@gmail.com> wrote:
>
>> Hi all
>>
>> I have two tables A and B
>> I have saved a record in table A and I want to save the id of that record in table B. How Can I do that.
>> Well I can do by search of same params which I save in Table A but I feel it is not that good approach.
>
> Just save it, then use the id ;-)
>
> Seriously, this is a very common idiom, and you'll find that rails takes care of it.
>

You may need to reload the object, as in

if @whatever.save
@whatever.reload
@another_thing.whatever_id = @whatever.id
@another_thing.save
end

That would only be true in a create method, because the ID isn't known until after the thing is saved. But if your relationships are declared in the usual way (has_many, belongs_to, etc.) then this plumbing is taken care of for you in most cases.

Walter

honey ruby

unread,
Sep 5, 2013, 12:12:49 PM9/5/13
to Ruby on Rails: Talk
Thanks for your quick responses. Does I get the same id if I use @whatever.reload.
if that model is accessed often and there are multiple users working on same model and I want table A id to be saved as new record in table B
Table A
id     name       email
878   Sam         s...@mail.in.com


as I save 878 record in Table A I want that 878 id to be saved as new record in table B
Table B
id       table_a_id
900     878

If in this scenario does reload helps me considering that there will me more hits to Table A by multiple users.



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/AB2F082D-434A-40A3-BD64-5B23B05B2761%40wdstudio.com.

Colin Law

unread,
Sep 5, 2013, 12:37:54 PM9/5/13
to rubyonra...@googlegroups.com
On 5 September 2013 17:12, honey ruby <emailtoh...@gmail.com> wrote:
> Thanks for your quick responses. Does I get the same id if I use
> @whatever.reload.
> if that model is accessed often and there are multiple users working on same
> model and I want table A id to be saved as new record in table B
> Table A
> id name email
> 878 Sam s...@mail.in.com
>
>
> as I save 878 record in Table A I want that 878 id to be saved as new record
> in table B
> Table B
> id table_a_id
> 900 878

As I suggested a little while ago when you first asked this question,
but did not reply to my response, you should consider doing this using
an association between the tables. Something like table_b belongs_to
table_a, table_a has_many table_bs. Then rails will do a lot of the
hard work for you. Almost always in rails if you are manipulating id
values then you are doing it wrong.

Colin

honey ruby

unread,
Sep 5, 2013, 1:56:12 PM9/5/13
to Ruby on Rails: Talk
Thanks for the response Colin my last mail was deleted some how anyway thanks for your reply. So If I use association between the tables

Table A                    Table B
has_many B and      belongs_to A

now I save a new record to A
@table_a = TableA.save
@table_b = ?
what should be my next line so that id of that saved record will save in Table B


And how should we approach this with out association if there is no rails only Ruby project

help me on this



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Colin Law

unread,
Sep 5, 2013, 5:06:00 PM9/5/13
to rubyonra...@googlegroups.com
On 5 September 2013 18:56, honey ruby <emailtoh...@gmail.com> wrote:
> Thanks for the response Colin my last mail was deleted some how anyway
> thanks for your reply. So If I use association between the tables
>
> Table A Table B
> has_many B and belongs_to A
>
> now I save a new record to A
> @table_a = TableA.save
> @table_b = ?
> what should be my next line so that id of that saved record will save in
> Table B

Really, you need to work through some good tutorials to get the basics
of rails. Also read the Rails Guides, in particular the one on
Associations.

>
>
> And how should we approach this with out association if there is no rails
> only Ruby project

Don't know.

Colin
> https://groups.google.com/d/msgid/rubyonrails-talk/CAOKUzCknCfEBTsiddykGqCb4K_goTpbG53bG%3DYuJFY75FHKx9A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages