Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Hey, tell me how to use your gem to update the model #22

Closed
kirpen opened this issue Jul 21, 2014 · 3 comments
Closed

Hey, tell me how to use your gem to update the model #22

kirpen opened this issue Jul 21, 2014 · 3 comments

Comments

@kirpen
Copy link

kirpen commented Jul 21, 2014

I have a column on which you can find the object in the database, such as "handle"
I correctly write the code to see if this object already exists, then update its attributes, if it is not, create a new one?

@kirpen
Copy link
Author

kirpen commented Jul 21, 2014

Offered wrap it in your gem.
For example, you can add an option to update the model for the column in which the values ​​are unique, and the parameter to check for the existence of pass through getter method in class import.

class EmployeeImporter < ActiveImporter::Base
imports Employee

column 'First name', :first_name
column 'Last name', :last_name
column 'Department', :department do |department_name|
Department.find_by(name: department_name)
end

transfer params to block check_update

check_update 'First name'
end

in the file ' active_importer / lib / active_importer / base.rb'

def check_update(check_column = false)
if check_column
# start find model by unique column
# if model exists, then start update_attributes
end
end
if you tell me how to write an update attributes, I wrap everything and do commit

@kirpen
Copy link
Author

kirpen commented Jul 22, 2014

I try like this code, but it is not best way, can you help write best?

on :row_processing do
p = Product.find_by_handle(row['handle'])
attrs = {}
attrs = { handle: row['handle'], title: row['title'], ............. next params ........... }
unless p.nil?
p.update_attributes attrs
skip_rows_if { true }
end
end

This code work, but this is bullshit :(

@kirpen
Copy link
Author

kirpen commented Jul 22, 2014

@kirpen kirpen closed this as completed Jul 22, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant