从创建的记录中获取ID并将其保存到控制器中的另一个记录

时间:2014-01-17 21:40:36

标签: ruby-on-rails methods model controller model-associations

我正在创建一个Rails应用程序,该应用程序根据搜索词从500px的API中提取照片,并将结果保存到数据库中。我有两个模型:照片和搜索。我需要获取创建的search_term的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 个答案:

答案 0 :(得分: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