Andru1989
Andru1989

Reputation: 180

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

Upvotes: 0

Views: 110

Answers (2)

sites
sites

Reputation: 21775

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.

Upvotes: 1

user2013222
user2013222

Reputation:

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)

Upvotes: 1

Related Questions