2017-10-30 13 views
-1

選択したform_tagパラメータですべての重複文字列を削除する最も良い方法を知っています。選択したパラメータ配列の重複文字列を削除します

私は.uniq.distinctを試しましたが、動作しません。

<%= select_tag(:city, options_for_select(@customers.where(user: current_user).collect{ |b| [b.city, b.city]}), {:prompt => "City", :class => "form-control select-search"}) %> 

マイselect_tag彼らは同じであっても、各顧客の都市を与えます。

+0

この '@のcustomers.where試してみてください(ユーザー:CURRENT_USER).distinct.collectを{| B | [b.city、b.city]} ' – Vishal

+0

助けてくれてありがとうございます。@Vishal –

+0

あなたは直面している問題を共有できますか? – Vishal

答えて

1

代わりにActiveRecordのクエリ結果のためにcollect(マップ)を使用して、あなたはpluckにそれが選択した属性を持つ配列を返します。この方法を使用することができ、その後、あなたは次のように、uniqを使用することができます。

Customer.where('user = ?', current_user).pluck(:city).uniq 
# ['city1', 'city2', 'city3'] 

注むしれとまた、一緒に行くことができます明確な、それはクエリにDISTINCT文を追加し、その結果にuniq方法を連鎖避ける:

Customer.where('user = ?', current_user).pluck('DISTINCT city') 
# ['city1', 'city2', 'city3'] 

Customer.where('user = ?', current_user).distinct.pluck(:city) 
# ['city1', 'city2', 'city3'] 
+0

私は "select_tag(:city、options_for_select(Bien.where( 'user =?'、current_user).pluck( 'DISTINCT city'))"を試してみました。 "演算子が存在しません:name = integer" –

+0

モデルのスキーマは定義されていますか? –

関連する問題