Why don't you pass the id of the created search_term to save_photos method.
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