P
P
Philipp2014-10-12 19:13:06
ruby
Philipp, 2014-10-12 19:13:06

How is data import done in Rails?

There is a music table, it stores data in the form

music
- artist_id
- album_id
- title
- length
- picture_id
- uploader_id
и т.д.

Accordingly, there are a number of dependent tables - artists, albums, etc. Ordinary relational schema.
Data in CSV looks in style
Артист,Альбом,Название,Длина, и т.д
With CSV parsing, I sort of figured it out, I found Roo. It is clear that there are no pictures in the file, this is for the future.
The uploader_id is a link to the user who is uploading the file.
Here's the catch in how the import is done with data spreading across different models. With the exception of duplication, etc.
I found an example , but there everything was with a primitive model, but here everything is somewhat more complicated.
It is interesting how this is done in principle. But it's better to have some specific example to read the code (if not very complicated).
I'm used to PHP & Perl all this is done manually, in Rails everything is a little more abstract and I don't always understand what's going on.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2014-10-13
@vsuhachev

For parsing, CSV from the standard ruby ​​library has always been enough for me.
In a similar situation, I did this:
I downloaded all csv files into memory, prepared indexes for linking (in Hash) and then using the create, build and save methods created objects by linking them.
Example:

transaction do

albums = {}

album_csv.each do |line|
  raise "errrrr" if albums.has_key? line.title
  albums[line.title] = Album.create!(title: line.title)
end

artist_csv.each do |line|
  artist = Artist.new(title: line.title)
  artist.album = albums[line.album] or fail "no album: #{line.album}"
  artist.save!
end

end

If the amount of data is large, then we do the same but upload the data not into memory, but into temporary tables in the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question