honey ruby
2013-Sep-05 15:47 UTC
After I save a record in table A I want to save the id of that record in table B Immediatly
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 -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/47f826c0-aa8c-4b7b-8dd8-bd999fb7540b%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Scott Ribe
2013-Sep-05 15:50 UTC
Re: After I save a record in table A I want to save the id of that record in table B Immediatly
On Sep 5, 2013, at 9:47 AM, honey ruby <emailtohoneyruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/AF6ACC83-B9B1-45A6-A740-B8D4C9CFAECE%40elevated-dev.com. For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Sep-05 15:55 UTC
Re: After I save a record in table A I want to save the id of that record in table B Immediatly
On Sep 5, 2013, at 11:50 AM, Scott Ribe wrote:> On Sep 5, 2013, at 9:47 AM, honey ruby <emailtohoneyruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/AB2F082D-434A-40A3-BD64-5B23B05B2761%40wdstudio.com. For more options, visit https://groups.google.com/groups/opt_out.
honey ruby
2013-Sep-05 16:12 UTC
Re: After I save a record in table A I want to save the id of that record in table B Immediatly
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 sam-J9XYGKgEagdBDgjK7y7TUQ@public.gmane.org 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. On Thu, Sep 5, 2013 at 9:25 PM, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> > On Sep 5, 2013, at 11:50 AM, Scott Ribe wrote: > > > On Sep 5, 2013, at 9:47 AM, honey ruby <emailtohoneyruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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 > > -- > 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/AB2F082D-434A-40A3-BD64-5B23B05B2761%40wdstudio.com > . > For more options, visit https://groups.google.com/groups/opt_out. >-- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAOKUzCkxDnUGBEKYSDkHv8hEN5AKO19WsZ1JUUek%3Ds9jhg-KKQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Sep-05 16:37 UTC
Re: After I save a record in table A I want to save the id of that record in table B Immediatly
On 5 September 2013 17:12, honey ruby <emailtohoneyruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 sam-J9XYGKgEagdBDgjK7y7TUQ@public.gmane.org > > > 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 878As 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 -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLs7jwBTiOh4dWLCrDEjDQOdfLceSYf9NY1qoTST8f%3DcqA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
honey ruby
2013-Sep-05 17:56 UTC
Re: After I save a record in table A I want to save the id of that record in table B Immediatly
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 On Thu, Sep 5, 2013 at 10:07 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 5 September 2013 17:12, honey ruby <emailtohoneyruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 sam-J9XYGKgEagdBDgjK7y7TUQ@public.gmane.org > > > > > > 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 > > -- > 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLs7jwBTiOh4dWLCrDEjDQOdfLceSYf9NY1qoTST8f%3DcqA%40mail.gmail.com > . > For more options, visit https://groups.google.com/groups/opt_out. >-- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAOKUzCknCfEBTsiddykGqCb4K_goTpbG53bG%3DYuJFY75FHKx9A%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Sep-05 21:06 UTC
Re: After I save a record in table A I want to save the id of that record in table B Immediatly
On 5 September 2013 18:56, honey ruby <emailtohoneyruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 BReally, 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 projectDon''t know. Colin> > help me on this > > > > On Thu, Sep 5, 2013 at 10:07 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> On 5 September 2013 17:12, honey ruby <emailtohoneyruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 sam-J9XYGKgEagdBDgjK7y7TUQ@public.gmane.org >> > >> > >> > 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 >> >> -- >> 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLs7jwBTiOh4dWLCrDEjDQOdfLceSYf9NY1qoTST8f%3DcqA%40mail.gmail.com. >> >> For more options, visit https://groups.google.com/groups/opt_out. > > > -- > 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/CAOKUzCknCfEBTsiddykGqCb4K_goTpbG53bG%3DYuJFY75FHKx9A%40mail.gmail.com. > > For more options, visit https://groups.google.com/groups/opt_out.-- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLt2wgmhDq7rP_%2Bh-V380yNaSLjG%2B1%3DHHV05cOvCH3Gs2A%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.