1 class ProviderArtist < ActiveRecord::Base
3 belongs_to :artist, :foreign_key => :demanders_artist_id
5 validates :provider_id, :presence => true, :numericality => true, :existence => true
6 validates :providers_artist_id, :presence => true, :numericality => true
7 validates :demanders_artist_id, :presence => true, :numericality => true
9 def self.get_one pid, paid
10 ProviderArtist.find_by_provider_id_and_providers_artist_id(pid, paid) || ProviderArtist.new
13 def modify_artist attr
25 def self.import pid, providers_artists
27 ProviderArtist.transaction do
28 providers_artists.each do |providers_artist_attr|
29 provider_artist = ProviderArtist.get_one(pid, providers_artist_attr['id'])
30 paid = providers_artist_attr['id']
31 providers_artist_attr.delete 'id'
32 demander_artist = provider_artist.modify_artist providers_artist_attr
33 if demander_artist.valid?
34 provider_artist.attributes = {:provider_id => pid, :providers_artist_id => paid, :demanders_artist_id => demander_artist.id}
35 unless provider_artist.save
36 res << providers_artist_attr
39 res << providers_artist_attr
42 raise ActiveRecord::Rollback if res.any?
44 ArtistImportResult.new res