0

データベース内のアイテムのオートコンプリート検索には、gemrails3-jquery-autocompleteを使用します。オートコンプリートは正常に機能していますが、生成されたクエリに別の条件を追加する必要があります。

検索された文字列を書き始めると、次のクエリが生成されます。

SELECT persons.id, persons.name FROM "persons" WHERE (LOWER(persons.name) ILIKE 'jo%') ORDER BY persons.name ASC LIMIT 10

これにより、名前が。で始まるすべての行が返されjoます。

しかし、どのようにしてすべての人を検索できますか?joたとえば、column active_person= 1?この目的のためのヘルパーやそのようなものはありますか?

ありがとうございました

4

3 に答える 3

2

@Andreiの答えは正しいと思いますが、私が探していたものではありません。@user984621も同じだと思います。私が探していたのは、クエリに条件を追加したいということです。これを読むことでhttps://github.com/crowdint/rails3-jquery-autocomplete/blob/master/lib/rails3-jquery-autocomplete/orm/active_record.rb

Railsのオートコンプリートは追加の条件をサポートしていることがわかりました。次のように、デフォルトの使用法に別のパラメーターを渡す必要があります。

autocomplete :user, :name, :where => { :role => 'employee'} 

これの代わりに

autocomplete :user, :name
于 2012-10-16T21:37:58.337 に答える
2

If I understood the context correctly, then active_person is a boolean that indicates whether the user is active or not.

If this behavior is desirable most of time - that you search for users/people that are only active then you could include a default_scope in your Person model like this:

class Person < ActiveRecord::Base
  default_scope where(:active_person => 1)
end

This way any query that is ever generated will be also checking that active_person = 1

More on default scopes here: http://apidock.com/rails/ActiveRecord/Base/default_scope/class

于 2012-07-12T03:13:45.657 に答える
0

WHERE (LOWER(persons.name) ILIKE 'jo%')に変更WHERE (LOWER(persons.name) ILIKE 'jo%') AND active_person = 1

たくさんの句をANDsでつなぎ合わせることができORます。

これは実際にはSQLに関する質問であることに注意してください。タイトルとタグは、少し誤解を招く可能性があります。

于 2012-07-11T22:18:34.443 に答える