2014-01-17 42 views
0

我創建了一個Rails應用程序,該應用程序根據搜索詞從500px的API中提取照片並將結果保存到數據庫。我有兩個模型:照片和搜索。我需要獲取創建的search_term的ID並將其保存到每張照片,因此我有它們之間的關聯。從創建的記錄中獲取ID並將其保存到控制器中的另一個記錄

這是照片模型。

class Photo < ActiveRecord::Base 

    self.per_page = 12 
    validates :uniqueid, :presence => true, :uniqueness => true 
    validates :name, :presence => true 
    validates :times_viewed, :presence => true 
    validates :rating, :presence => true 
    validates :votes_count, :presence => true 
    validates :favorites_count, :presence => true 
    validates :image_url, :presence => true, :uniqueness => true 

    has_one :search 

end 

這是搜索模型。

class Search < ActiveRecord::Base 

    validates :search_term, :presence => true 
    has_many :photos 

end 

我需要跟蹤的我搜索過一定期限的次數,所以我可以保證我不會每次都得到結果的同一頁回來。

用於獲取照片的控制器看起來是這樣的:

def index 
    @search = params[:search] 

    if @search 
    # I need to fetch the id of this search term 
    Search.create search_term: @search 
    @json_response = JSON.parse(get_access_token.get("/v1/photos/search?term=#{CGI.escape @search}&rpp=100&image_size=4&sort=times_viewed").body) 
    save_photos @json_response 
    end 

end 

從本質上講,我需要做的是獲取創建的搜索項的ID,並保存到此save_photos方法中的每一張照片。

def save_photos json_response 

    json_response['photos'].each do |photo| 
    Photo.create uniqueid: photo['id'], 
     name: photo['name'], 
     description: photo['description'], 
     times_viewed: photo['times_viewed'], 
     rating: photo['rating'], 
     votes_count: photo['votes_count'], 
     favorites_count: photo['favorites_count'], 
     image_url: photo['image_url'], 
     photo_taken: photo['created_at'], 
     category: photo['category'], 
     privacy: photo['privacy'], 
     comments_count: photo['comments_count'], 
     nsfw: photo['nsfw'], 
     # I’d like to save the id of the search term here 
     Search.create search_id: @search 
    end 

end 

道歉,如果這是一個重複的問題或者是驚人地簡單 - 我已經到了窮途末路與知道該怎麼尋找這個問題。

回答

1

爲什麼不將創建的search_term的id傳遞給save_photos方法。

def index 
    @search = params[:search] 

    if @search 
    # I need to fetch the id of this search term 
    search = Search.create search_term: @search 
    @json_response = JSON.parse(get_access_token.get("/v1/photos/search?term=#{CGI.escape @search}&rpp=100&image_size=4&sort=times_viewed").body) 
    save_photos(@json_response, search.id) 
    end 

end 

def save_photos(json_response, search_id) 

    json_response['photos'].each do |photo| 
    Photo.create uniqueid: photo['id'], 
     name: photo['name'], 
     description: photo['description'], 
     times_viewed: photo['times_viewed'], 
     rating: photo['rating'], 
     votes_count: photo['votes_count'], 
     favorites_count: photo['favorites_count'], 
     image_url: photo['image_url'], 
     photo_taken: photo['created_at'], 
     category: photo['category'], 
     privacy: photo['privacy'], 
     comments_count: photo['comments_count'], 
     nsfw: photo['nsfw'], 
     # I’d like to save the id of the search term here 
     search_term_id: search_id 
    end 

end 
+0

完成了這個技巧,謝謝! – gosseti

相關問題