Search code examples
ruby-on-railsmethodsmodelcontrollermodel-associations

Fetch ID from created record and save it to another record in controller


I’m creating a Rails app that fetches photos from 500px’s API based on a search term and saves results to the db. I have two models: Photo and Search. I need to fetch the ID of the created search_term and save that to each photo, so I have an association between them.

Here is the Photo model.

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

Here is the Search model.

class Search < ActiveRecord::Base

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

end

I need to keep track of the number of times I’ve searched for a certain term, so I can ensure I’m not getting the same page of results back each time.

The controller for fetching the photos looks like this:

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

Essentially, what I need to do is fetch the id of the created search term, and save that to each photo within this save_photos method.

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

Apologies if this is a duplicate question or is strikingly simple — I’ve come to a dead end with knowing what to search for with this problem.


Solution

  • 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