【问题标题】:Fetch ID from created record and save it to another record in controller从创建的记录中获取 ID 并将其保存到控制器中的另一条记录
【发布时间】:2014-01-17 21:40:36
【问题描述】:

我正在创建一个 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

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      相关资源
      最近更新 更多