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

Ruby on rails 从创建的记录中获取ID,并将其保存到控制器中的另一个记录中,ruby-on-rails,methods,model,controller,model-associations,Ruby On Rails,Methods,Model,Controller,Model Associations,我正在创建一个Rails应用程序,它根据搜索词从500px的API获取照片,并将结果保存到db。我有两个模型:照片和搜索。我需要获取创建的搜索词的ID并将其保存到每张照片中,这样它们之间就有了关联 这是照片模型 class Photo < ActiveRecord::Base self.per_page = 12 validates :uniqueid, :presence => true, :uniqueness => true validates :name,

我正在创建一个Rails应用程序,它根据搜索词从500px的API获取照片,并将结果保存到db。我有两个模型:照片和搜索。我需要获取创建的搜索词的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
本质上,我需要做的是获取创建的搜索词的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
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

如果这是一个重复的问题或是非常简单的问题,我深表歉意-我已经走到了一条死胡同,不知道用这个问题搜索什么。

为什么不将创建的搜索项的id传递给save\u 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
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