2015-01-07 5 views
0

.rake 파일을 사용하여 레코드를 저장하려고합니다. 하지만 변경 사항을 저장하려고 할 때 오류가 발생합니다.활성 레코드를 사용하여 저장하는 중 오류가 발생했습니다.

하고 Error.message

SQLite3::ConstraintException: UNIQUE constraint failed: versions.id: INSERT INTO "versions" ("app_id", "created_at", "icon_url", "id", "plist_url", "updated_at", "version_number") VALUES (?, ?, ?, ?, ?, ?, ?) 

inbox.rake 파일

namespace :inbox do 
    desc 'Check inbox for new app builds' 
    task process_inbox: :environment do 
    # initialize s3 client 
    s3 = AWS::S3.new 
    bucket = s3.buckets['my-bucket'] 
    s3_host = 'https://...s3.amazonaws.com/' 

    exclude_inbox = bucket.objects.select do |s3_object| 
     s3_object.key.exclude? '_inbox' 
    end 
    #find plist within the inbox 
    plist_objects = exclude_inbox.select do |s3_object| 
     s3_object.key.include? 'plist' 
    end 

    for plist_object in plist_objects 
     plist = CFPropertyList::List.new(:data => plist_object.read) 
     data = CFPropertyList.native_types(plist.value) 

     name = data['items'][0]['metadata']['title'] 
     bundle_version = data['items'][0]['metadata']['bundle-version'] 
     plist_copy = plist_object.copy_to("#{name}/#{bundle_version}/#{name}.plist") 
     kind = data['items'][0]['metadata']['kind'] 

     plist = CFPropertyList::List.new(:data => plist_copy.read) 
     data = CFPropertyList.native_types(plist.value) 
     icon_url = data['items'][0]['assets'][1]['url'] 
     full_url = plist_copy.url_for(:read) 

     icon = bucket.objects[icon_url.gsub(s3_host, '')] 

     #find app or create a new app based on its name and kind 
     app = App.find_or_initialize_by(name: name, app_type: kind) 
     #app.save unless app.id 
     #find version or create new version base on app_id and bundle_version 
     version = Version.find_or_initialize_by(app_id: app.id, id: bundle_version) 
     version.plist_url = full_url.scheme + '://' + full_url.host + full_url.path 
     version.icon_url = icon.copy_to("#{name}/#{bundle_version}/#{icon_url.split('/').last.gsub('~','_')}").url_for(:read).to_s 
     version.version_number = bundle_version 
     version.app = app 
     #update app version number 
     app.version_number = version.version_number 
     #save changes 
     begin 
     app.save 
     version.save 
     puts app.attributes , version.attributes 
     rescue => error 
     puts error.message 
     end 
    end 

    end 
end 
+0

오류 메시지에 모든 UNIQUE 제약 조건이 실패했습니다. 'versions.id'는 유일해야하며, 이미 존재하는 id를 가진 레코드를 삽입하려고합니다. – RocketR

+0

@RocketR 저도 이해합니다. 그러나 그 이유는 모르겠습니다. 이제 내 코드에서 수동으로 ID를 할당합니다. –

답변

1

이가있는 새 버전을 얻을 것이다 문제 라인 :

version = Version.find_or_initialize_by(app_id: app.id, id: bundle_version) 

이것은 더 자세한하지만 아마 더 당신이 원하는 :

version = Version.find_by(id: bundle_version, app_id: app.id) || Version.new(app_id: app.id) 
+0

@ Vimsha 내가 원했던 것은 사실 이었지만'version = Version.find_or_initialize_by (version_number : bundle_version, app_id : app.id)'때문이었습니다. 당신 덕분에 –

1

이 줄은 기본 키가 여기에 내가 ID를 추측하고있는 문제

Version.find_or_initialize_by(app_id: app.id, id: bundle_version) 

입니다. 당신이 find_or_initialize 두에 값을하고 있기 때문에 주어진 bundle_version하지만 서로 다른 APP_ID와 데이터베이스의 버전이있을 때, 당신은

당신은 아마 일을해야

,

version = Version.find_or_initialize_by(id: bundle_version) 
version.app_id = app.id 
+0

우리의 대답은 얼마나 비슷한가! 하지만 당신이 머리카락으로 레이스에서 우승했다고 생각합니다. :-) – evanbikes

관련 문제