I am creating a record in a table and after creation I want to create a record in another table

20 views
Skip to first unread message

Jagmeet Singh

unread,
Apr 29, 2015, 4:42:05 AM4/29/15
to rubyonra...@googlegroups.com
I am inserting a record in 1st table named as Students and I want to insert a record  in another table with a value coming from a column Refid which is in Students table

Colin Law

unread,
Apr 29, 2015, 4:59:36 AM4/29/15
to rubyonra...@googlegroups.com
You can create the record using something like
Tablename.create(...)
By the way, the column should be refid or possibly ref_id, it is best
to stick to the rails naming conventions.

However if the field refid exists in both tables that strongly
suggests there should be some relationship between the tables and you
need to sort that out first.

If you explain the purpose of the tables we may be able to suggest a
better way of doing it.

Also the fact that you have to ask the question suggests that maybe
you are a beginner in Rails, in which case I suggest you take a few
days out to work right through a good tutorial such as
railstutorial.org (which is free to use online). The time spent will
be recovered very quickly.

Cheers

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-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/473064e8-3e10-471a-8a57-c51a352ba9f4%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Elizabeth McGurty

unread,
Apr 30, 2015, 10:18:28 AM4/30/15
to rubyonra...@googlegroups.com

I don't know if this is an acceptable practice, but I do the following on update... and it works for me, especially as I have not been been to get table associations to work.


def update

unless params[:id].blank?
unless params[:parent_object].blank?

   @p_o_hold = ParentObject.find(params[id])
   unless @p_o_hold.blank?
    myupdatehash = Hash.new
    /////  Then I select from params[:parent_object] that I need and apply some validation or database format requirements
        myupdatehash = [:field => ...., ]
    if @p_o_hold.update_attributes(myupdatehash[0])   
        @child_object = ChildObject.find(@p_o_hold.id)
         unless  @child_object.blank?
            myupdatehash = [:child_field => @p_o_hold.child_field_value]   ## hash object reuse
                @child_object.update_attributes(myupdatehash[0])

.....

Chris Lerum

unread,
Apr 30, 2015, 11:50:10 AM4/30/15
to rubyonra...@googlegroups.com
It's to your credit that you got that working, but it's far from conventional - why not post your attempt to get associations working, with your expected result vs. actual, and you'll soon be greeted with a nice 'AHA!' moment. I bet.

Elizabeth McGurty

unread,
Apr 30, 2015, 12:16:58 PM4/30/15
to rubyonra...@googlegroups.com
Very, very kind.... I do now feel very motivated and it seems that I have finally discovered a support system.  May take me days to restructure my site www.echomarket.org to prove or demonstrate my difficulties. Otherwise I thank you sincerely.
Liz
Reply all
Reply to author
Forward
0 new messages