Rails针对表行保存id数组

时间:2017-10-11 14:59:45

标签: ruby-on-rails

所以我有一堆与event匹配的数组。我有一个名为ticketmaster的表,它返回6个这样的ID:

#<ActiveRecord::Relation [#<Ticketmasters id: 1>, #<Ticketmasters id: 1106>, #<Ticketmasters id: 1317>, #<Ticketmasters id: 4904>, #<Ticketmasters id: 11648>, #<Ticketmasters id: 13061>]>

现在我想要的是保存event表中的events

我的最终目标是从ticketmasters表中获取价格,并在视图中以循环方式显示它们。我正在努力研究如何全面解决这个问题?

萨姆

event.rb

class Event < ActiveRecord::Base
  has_permalink :eventurl
  has_many :skiddle_tickets

  def eventurl
    "AM#{custom_event_id.scan(/.{1,4}/)[0]}" + "#{custom_event_id.scan(/.{1,4}/)[1]}" + "#{custom_event_id.scan(/.{1,4}/)[2]}" + "#{custom_event_id.scan(/.{1,4}/)[3]}"
  end

  is_impressionable
  searchkick suggest: [:eventname], batch_size: 1000, word_start: [:eventname]
end

ticketmaster.rb

class Ticketmasters < ActiveRecord::Base

  scope :search_promotional_text, -> (text)         { where("promotional_text ILIKE ?", "%#{text}%") }
  scope :search_delivery_time,    -> (time)         { where("delivery_time ILIKE ?", time)}
  scope :search_product_name,     -> (product_name) { where("product_name ILIKE ?", "%#{product_name}%") }
  scope :search_valid_from,       -> (date)         { where("valid_from = ?", date) }

    class << self

      def search_by(search_options={})
        search_result = nil
        search_options.each do |k,v|
          search_result = (search_result ? search_result : self).send("search_#{k}",v)
        end
        search_result
      end

    end
end

0 个答案:

没有答案