使'uniq'基于关联返回不同的数组

时间:2017-03-19 13:21:52

标签: ruby-on-rails activerecord

我有以下模特:训练营,教练和国家。营地有一个国家和教练也有一个国家。

这样的事情:

Camp Super Fun(国家:丹麦)和 Camp No Fun(国家:英格兰)

Foo教练(国家:意大利)和 教练酒吧(国家:德国)

我有一个简单的搜索,用户可以根据国家/地区过滤营地和教练。当用户过滤营地时,我只希望营地国家出现在下拉列表中(丹麦和英格兰)。当用户通过Coach过滤时,我希望教练国家出现在下拉列表中(意大利和德国)。

如何让教练过滤器只有教练国家选项,而营地过滤器只有营地国家?

现在,我只获得两个过滤器中所有国家/地区的列表。这是我的模型和控制器:

Camp.rb:

class Camp < ApplicationRecord
  has_and_belongs_to_many :coaches
  has_and_belongs_to_many :countries
  dragonfly_accessor :image
  def self.search(price, country, coach, start_date, end_date, city, language)

    camps = Camp.where("1 = 1")

    camps = camps.where("price <= ?", price) unless price == nil
    camps = camps.where("start_date >= ?", start_date) unless start_date == nil
    camps = camps.where("end_date <= ?", end_date) unless end_date == nil
    camps = camps.where("city LIKE ?", city) unless city == nil

    if country.present?
      camps = camps.joins(:countries).where(countries: { name: country })
    end

    if coach.present?
      camps = camps.joins(:coaches).where(coaches: { name: coach })
    end

    if language.present?
      camps = camps.joins(coaches: :languages).where(languages: { language_code: language })
    end

    camps
  end

  def languages
    coaches.flat_map { |coach| coach.languages.map(&:name)}
  end
end

Coach.rb:

class Coach < ApplicationRecord
  dragonfly_accessor :image
  has_and_belongs_to_many :camps
  has_and_belongs_to_many :languages
  has_and_belongs_to_many :athletes
  has_and_belongs_to_many :countries

  def self.search(country, city)

    coaches = Coach.where("1 = 1")

    if country.present?
      coaches = coaches.joins(:countries).where(countries: { name: country })
    end

    coaches = coaches.where("city LIKE ?", city) unless city == nil

    coaches
  end
end

Country.rb:

class Country < ApplicationRecord
  has_and_belongs_to_many :camps
  has_and_belongs_to_many :coaches
end

coaches_controller.rb:

class CoachesController < ApplicationController

  def index
    @featured_coaches = Coach.where(featured: true)
  end

  def search
    @coach_country = Country.uniq
    @coach_city = Coach.uniq.pluck(:city)

    country = params[:country].present? ? params[:country] : nil
    city = params[:city].present? ? params[:city] : nil

    @coaches = Coach.search(country, city)
  end

end

camps_controller.rb

class CampsController < ApplicationController

  def index
    @featured_camps = Camp.where(featured: true)
    @camps_on_map = Camp.where(show_on_maps: true)
    @camp_count = Camp.count.to_s
  end

  def search
    @camp_coach = Coach.all.pluck(:name)
    @camp_country = Country.uniq
    @camp_city = Camp.uniq.pluck(:city)
    @camps = Camp.all.order('created_at DESC')
    @camp_language = Language.uniq

    price = params[:price].present? ? params[:price] : nil
    country = params[:country].present? ? params[:country] : nil
    coach = params[:coach].present? ? params[:coach] : nil
    start_date = params[:start_date].present? ? params[:start_date] : nil
    end_date = params[:end_date].present? ? params[:end_date] : nil
    city = params[:city].present? ? params[:city] : nil
    language = params[:language].present? ? params[:language] : nil

    @camps = Camp.search(price, country, coach, start_date, end_date, city, language)
  end
end

0 个答案:

没有答案