Rails выбирает дату из доступности

Я работаю над приложением, я фактически реализую фильтр в своем пользовательском индексе для поиска некоторых пользователей по: языку, городу и доступности (дате). Фильтр по языку и городу работает отлично, но не по дате.

index.html.erb :

<div>
  Date de départ :
  <%= f.text_field :with_start_time_gte, 'data-provide' => 'datepicker' %>
</div>
<%= f.submit 'Rechercher' %>

Я создаю модель Availability с параметрами (:name, :start_time, :end_time, :user_id). Мне нужно, чтобы фильтр выполнял поиск между датой между start_time и end_time доступности и возвращал доступных пользователей.

users_controller.rb :

  def index
@filterrific = initialize_filterrific(
  User,
  params[:filterrific]
  #persistence_id: 'shared_key',
  #default_filter_params: {},
  #available_filters: [],
) or return
@users = @filterrific.find.page(params[:page])

# Respond to html for initial page load and to js for AJAX filter updates.
respond_to do |format|
  format.html
  format.js
end

rescue ActiveRecord::RecordNotFound => e
  # There is an issue with the persisted param_set. Reset it.
  puts "Had to reset filterrific params: #{ e.message }"
  redirect_to(reset_filterrific_url(format: :html)) and return
end

пользователь.рб :

  filterrific(
default_filter_params: {},
available_filters: [
  :with_mother_tongue,
  :with_locality,
  :with_start_time_gte])

scope :with_mother_tongue, -> (search_string) {
where("users.mother_tongue LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}

scope :with_locality, -> (search_string) {
where("users.locality LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}

scope :with_start_time_gte, lambda { |ref_date|
where('users.availabilities.start_time >= ?', ref_date)
}

Я думаю, что проблема в том, чтобы взять start_time из доступности пользователя... Но не знаю, как это сделать... Заранее спасибо за вашу помощь!


person Benjamin Richard    schedule 19.06.2018    source источник


Ответы (1)


Во-первых, ваши отношения между User и Availability неверны. Отношение HABTM должно быть общим между двумя моделями с таблицей соединения (источник )
Измените отношение на has_many/belongs_to, если Доступность принадлежит только одному Пользователю.

Для вашей области вы должны присоединиться к отношениям в запросе:

scope :with_start_time_gte, -> (ref_date) { joins(:availabilities).where('availabilities.start_time >= ?', ref_date) }
person Sovalina    schedule 19.06.2018
comment
Я уже создал CreateJoinTableUsersAvailabilities, поставил User has_many Availability и поставил вашу область видимости, и все заработало! Спасибо за помощь ! - person Benjamin Richard; 20.06.2018