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
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
You can create the record using something like
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.
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.
@p_o_hold = ParentObject.find(params[id])
myupdatehash = Hash.new
///// Then I select from params[:parent_object] that I need and apply some validation or database format requirements
myupdatehash = [:field => …, ]
@child_object = ChildObject.find(@p_o_hold.id)
myupdatehash = [:child_field => @p_o_hold.child_field_value] ## hash object reuse
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.
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.