I am creating a record in a table and after creation I want to create a record in another 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

On 29 April 2015 at 09:17, Jagmeet S. [email protected]
wrote:

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

I don’t know if this is an acceptable practice, but I do the following
on
update… and it works for me

def update

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

@p_o_hold = @parent_object.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])

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

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])