2017-04-04 63 views
0

對不起,這可能很簡單,但它讓我難以忍受我的頭撞牆!自定義搜索belongs_to關聯爲空

我創建了我自己的搜索模型和控制器,並且無法搜索belongs_to關聯。我能夠很好地搜索has_and_belongs_to_many關聯,但我得到了belongs_to的零結果。

# models/user.rb 
class User < ApplicationRecord 
    belongs_to :city 

    has_and_belongs_to_many :sports 
    has_and_belongs_to_many :goals 
end 

# models/search.rb 
class Search < ApplicationRecord 
    def search_users 
    users = User.all 
# These two work 
    users = users.joins(:sports).where("sports.name ILIKE ?", "%#{sports}%") if sports.present? 
    users = users.joins(:goals).where("goals.name ILIKE ?", "%#{goals}%") if goals.present? 

# This doesn't 
    users = users.joins(:city).where("cities.name ILIKE ?", "%#{cities}") if cities.present? 

    return users 
    end 
end 

# searches_controller.rb 
class SearchesController < ApplicationController 
    def new 
    @search = Search.new 
    @sports = Sport.uniq.pluck(:name) 
    @goals = Goal.uniq.pluck(:name) 
    @cities = City.uniq.pluck(:name) 
    end 

    def create 
    @search = Search.create(search_params) 
    redirect_to @search 
    end 

    def show 
    @search = Search.find(params[:id]) 
    end 

    private 
    def search_params 
    params.require(:search).permit(:sports, :goals, :gyms, :cities) 
    end 
end 

和我的看法:

#searches/new.html.erb 
<%= simple_form_for @search do |search| %> 
    <%= search.label :sports %> 
    <%= search.select :sports, options_for_select(@sports), include_blank: :true %> 

    <%= search.label :goals %> 
    <%= search.select :goals, options_for_select(@goals), include_blank: :true %> 


    <%= search.input :cities, collection: City.all.order(name: :asc), as: :select, label: "City" %> 

    <%= submit_tag "Search" %> 
<% end %> 

搜索/ show.html.erb

<% if @search.search_users.empty? %> 
    Nothing 
<% else %> 

    <% @search.search_users.each do |user| %> 
    <%= user.first_name + ' ' + user.last_name %> 

    <% end %> 

<% end %> 

基本上從下拉列表中不註冊選擇城市的時候,給我的空搜索條件,我不明白爲什麼它不會選擇它。然而,選擇運動和/或目標,它給了我匹配的用戶。

在此先感謝!

編輯:

好吧,我已經成功地得到它的工作通過簡單地改變在我看來我的選擇下拉菜單:

<%= search.input :cities, collection: City.all.order(name: :asc), as: :select, label: "City" %> 

這樣:

<%= search.select :cities, options_for_select(@cities) %> 

然而,我仍然想知道爲什麼其他方式不起作用?

+0

我想'如果cities.present?'應該是'如果city.present?' – Sravan

+0

嘿,對不起,沒有給出未定義的變量/方法錯誤。我的控制器中允許使用參數。我現在加了它,對不起,我忘了! –

+0

是的,剛剛看到。將'@ search.search_users'移動到控制器。另外, '<%= @ search.search_users.each do | user | %>'應該是'<%@ search.search_users.each do | user | %>' – Sravan

回答

1
<%= search.input :cities, collection: City.all.order(name: :asc), as: :select, label: "City" %> 

其中一個上面的代碼沒有工作的原因,

City.all.order(name: :asc)回報ActiveRecord::Relation對象,而是一個集搜索的array or range.

集合可以是陣列或範圍。從the collection documentation

從該文件建立的另一點是,

:collection給出:select輸入將被默認渲染,所以我們並不需要通過as: :select

所以,變化輸入到

<%= search.input :cities, collection: City.uniq.pluck(:name).sort,label: "City" %>

+0

就是這樣。謝謝!愚蠢到最後..我可以問你如何按字母順序排列名字?因爲這是我以另一種方式嘗試的主要原因。 –

+0

'City.uniq.pluck(:name).sort'檢查更新後的答案。 – Sravan