save information from another table - ruby-on-rails

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

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)

Related

rails 4 conditions where value from table

I have created a table with a attribute called category. The category has different values 'airport', 'sights' ect.
How can i find all locations in the category 'sights' ?
i tried this:
#sights_markers = Location.where(:category = 'sights')
and this
#sights_markers = Location.where('category = sights')
and this
#sights_markers = Location.where('category = sights')
but this give me a error.
Please advise..thanks..remco
Its simple,
#sights_markers = Location.where(category: 'sights')
Please read this at least once. http://guides.rubyonrails.org/active_record_querying.html
you have make a mistake, do like this,it will work:
#sights_markers = Location.where(:category => 'sights')
or
#sights_markers = Location.where("category = 'sights'")

Creating a record from a third-party object with same field names?

I have a Charge model in my database with field names that match up to the fields returned from a third-party API.
charge = ThirdPartyChargeAPI.find(1)
Charge.create do |e|
e.object = charge.object
e.paid = charge.paid
e.amount = charge.amount
e.currency = charge.currency
e.refunded = charge.refunded
e.amount_refunded = charge.amount_refunded
e.failure_message = charge.failure_message
e.failure_code = charge.failure_code
e.description = charge.description
e.metadata = charge.metadata
e.captured = charge.captured
e.balance_transaction = charge.balance_transaction
e.customer = charge.customer
e.invoice = charge.invoice
e.created = charge.created
end
Seems painfully redundant, though. Is there some way to merge this without having to basically set every single field manually?
Assuming there's no way to get a direct hash from the API (I would imagine there would be, since it's probably coming in as XML or JSON), you could try a direct map of instance variables:
Charge.create do |c|
charge.instance_variables.each do |var|
value = charge.instance_variable_get(var)
c.instance_variable_set(var, value)
end
end
This is making some pretty bold assumptions about the structure of the charge you're getting back from the API though - any instance variable in it that you don't want will be included.

Ruby on Rails merge several objects into one

I have following problem:
I have a Class named Foo and some instances of this Class like this:
#foo1
#foo2
#foo3
Each of these instances has an attribute called :description with a text in it like this:
#foo1.description = "Lorem"
#foo2.description = "ipsum"
#foo3.description = "dolore"
Now I would like to merge/combine the three objects above so that I afterward have only one instance like this:
#foo1.description = "Lorem ipsum dolore"
Has anyone an idea how i could do this merging?
Thanks.
EDIT MORE CONTEXT:
I have a class named Risk and a lot of instances.
The attributes of an instance of the Risk class are id, description, issue, references and target_ids as an instance of the Risk class can has_many targets.
In my Risk Index view where it displays all Risks I'd like to have an additional checkbox column where i can check all the risks I'd like to merge. Then there is a button called "Merge" and if the button is pressed all the Risks which are checked in the checkbox should be merged into the first one which is checked.
#foo1.description = "Lorem"
#foo2.description = "ipsum"
#foo3.description = "dolore"
#foos = #foo1 + #foo2 + #foo3
desc_array = []
#foos.each do |foo|
desc_array << foo.description
end
#foo1.description = desc_array.join
#foo1.description.save()
you could do following:
descriptions = Foo.where(:id => [#foo1, #foo2, #foo3]).map(&:description)
new_description = descriptions.join
#foo1.description = new_description
#foo1.save!
#foo2.destroy
#foo3.destroy

getting a column by "name" in an activerecord

I have a select statement that returns a field in a table.
records = Product.select("DISTINCT #{iFieldName}").where("id in (0, #{iInClaws})" )
I want to convert the data in the database to an array to be used later.. say as another inClaws.
records.each{|record| fieldArray << record.?????}
I have two questions.
Is there a better way to do this?
If there's not, then how do I get the data out of the field.
Thanks to all..
After everyone came to help my final method looked like this.
def self.getFieldArray(iFieldName, iIDsInClaws, iIdFieldName = 'id')
records = self.select("DISTINCT #{iFieldName}").where("#{iIdFieldName} in (#{iIDsInClaws})" )
return records.map{|record| record.send(iFieldName)};
end
fieldArray = records.map(&:iFieldName)
Or
res = records.map{|r| r.send(iFieldName)}
records.each{|record| fieldArray << record.send(iFieldName)}

Ruby on rails save data inside loop

object.each do |info|
#user_work_history.fb_user_id = facebook_session.user.id
#user_work_history.city = info.location.city
#user_work_history.country = info.location.state
#user_work_history.company_name = info.company_name
#user_work_history.description = info.description
#user_work_history.start_date = info.start_date
#user_work_history.end_date = info.end_date
#user_work_history.position = info.position
#user_work_history.updated_at = Time.now
#user_work_history.save
end
here is the code but inside loop data is not saving only one record is saved.
I believe you have to add
#user_work_history = UserWorkHistory.new
to the first line inside the loop. (or whatever the model is)
When you do that kind of loop you need to change the reference to the instance variable #user_work_history. What your loop does is that for each iteration it updates the state of the same object!
What you should do, is to set the #user_work_history before each iteration, like this:
object.each do |info|
#user_work_history = UserWorkHistory.new(:fb_user_id => facebook_session.user.id) # for example
#user_work_history.city = info.location.city
#user_work_history.country = info.location.state
(...)
#user_work_history.save # here it saves new tuple
end

Resources