简体   繁体   中英

save information from another table

Hi everyone at this time i have two tables:

clientesultimasgestiones clientesgestiones

And I want to put the whole information from clientesgestiones to clientesultimasgestiones but I want to save it field by field, at this momento I have this

cnx = ActiveRecord::Base.connection
cnx.execute("truncate table clientesultimasgestiones")
@informacion = Clientesgestion.all

    @informacion.each do |f|
        @clientesultimasgestion = Clientesultimasgestion.new
        @clientesultimasgestion.save(f)
        Here will be the code to save field by field from clientesgestiones table to the another one
    end

Thanks for your help

EDIT: Finally i did it this way:

    cnx.execute("truncate table clientesultimasgestiones")
    @informacion = Clientesgestion.all

    @informacion.each do |f|
        l                               = Clientesultimasgestion.new
        l.persona_id                    = f.persona_id
        l.fecha_gestion                 = f.fecha_gestion
        l.clientestipologia_id          = f.clientestipologia_id
        l.observacion                   = f.observacion
        l.user_id                       = f.user_id
        l.fecha_acuerdo                 = f.fecha_acuerdo
        l.valor_apagar                  = f.valor_apagar
        l.clientestipologiaanterior_id  = f.clientestipologiaanterior_id
        l.clientesobligacion_id         = f.clientesobligacion_id
        l.save

    end

Thanks a lot :)

I think this question will help you to get lists of attributes and values.

After this, you need to set dynamically fields, for this purpose you can use method send . Something like this: @clientesultimasgestion.send("#{field_name}=",field_value)

I would replace:

    @clientesultimasgestion.save(f)

with:

    @clientesultimasgestion.update_attibutes(f.attributes)

Also, seems what you want is to copy a table, see https://stackoverflow.com/a/13237661/1197775 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM