# == Schema Information
#
# Table name: search_records
#
#  id         :integer          not null, primary key
#  created_at :datetime         not null
#
# Indexes
#
#  index_search_records_on_created_at  (created_at)
#

class SearchRecord < ActiveRecord::Base
  STORAGE_LIFETIME = 90.days

  has_many :search_records_words, dependent: :destroy
  has_many :words, -> { order('search_records_words.position') }, through: :search_records_words

  scope :lifetime_expired, -> { where 'created_at < ?', STORAGE_LIFETIME.ago }

  def self.create_from_search(search)
    search_record = create
    search.fragments.each do |fragment|
      search_record.search_records_words.create word: fragment.word,
        position: fragment.position,
        operation: fragment.operation
    end

    search_record
  end
end

# == Schema Information
#
# Table name: search_records
#
#  id         :integer          not null, primary key
#  created_at :datetime         not null
#
# Indexes
#
#  index_search_records_on_created_at  (created_at)
#

class SearchRecord < ActiveRecord::Base
  STORAGE_LIFETIME = 30.days

  has_many :search_records_words, dependent: :destroy
  has_many :words, -> { order('search_records_words.position') }, through: :search_records_words

  scope :lifetime_expired, -> { where 'created_at < ?', STORAGE_LIFETIME.ago }

  def self.create_from_search(search)
    search_record = create
    search.fragments.each do |fragment|
      search_record.search_records_words.create word: fragment.word,
        position: fragment.position,
        operation: fragment.operation
    end

    search_record
  end
end

Reduce SearchRecord::STORAGE_LIFETIME to 30 days
Reduce SearchRecord::STORAGE_LIFETIME to 30 days