Question

J'ai un ensemble des recherches de photos, et je veux limiter les résultats de la page d'index à ceux qui sont publics (ne pas appartenir à une collection privée, alias. :collection_id=>nil).

J'ai la portée suivante dans mon modèle:

scope :community, where(:collection_id => nil)

Je suit dans mon contrôleur:

def index
  @search = Photo.community.search(params[:search])
  @photos = @search.page(params[:page]).per(20)
end

J'utilise Kaminari paginer les résultats.

Quand je recherche des photos non délimité (c.-à-Photo.search(params[:search])) il fonctionne très bien. Cependant, quand je cherche des photos à l'aide de cette pagination étendue casse la recherche. La première page affiche correctement, mais quand je vais à une autre page, il n'y a pas de résultats. Pour tester cette place j'ai changé à 1 par la pagination et la page toujours, peu importe ce que je cherche des pages après la première page montrent aucun résultat même si le paginator indique qu'il ya beaucoup plus de pages de résultats.

Je ne sais pas comment déboguer et résoudre ce problème, et je voudrais vraiment utiliser un peu d'aide. Voici le SQL à partir du journal de développement:

Pour une charge normale de la page d'index (pas de recherche):

Started GET "/photos" for 127.0.0.1 at 2011-04-28 11:58:51 -0500
  Processing by PhotosController#index as HTML
  SQL (0.4ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

  SQL (0.3ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Context Load (0.3ms)  SELECT "contexts".* FROM "contexts" ORDER BY "contexts"."display_order" ASC
  Focus Load (0.3ms)  SELECT "focus".* FROM "focus" ORDER BY "focus"."display_order" ASC
  LandUse Load (0.3ms)  SELECT "land_uses".* FROM "land_uses" ORDER BY "land_uses"."display_order" ASC
Rendered static/_help-context.html.haml (4.5ms)
Rendered static/_help-focus.html.haml (3.1ms)
Rendered photos/_search.html.haml (304.9ms)
  SQL (0.5ms)  SELECT COUNT(DISTINCT "photos"."id") FROM "photos" LEFT OUTER JOIN "attachments" ON "attachments"."photo_id" = "photos"."id" LEFT OUTER JOIN "collections" ON "collections"."id" = "photos"."collection_id" WHERE "photos"."collection_id" IS NULL LIMIT 1 OFFSET 0
  Photo Load (1.0ms)  SELECT "photos".* FROM "photos" WHERE "photos"."collection_id" IS NULL LIMIT 1 OFFSET 0
  Attachment Load (0.2ms)  SELECT "attachments".* FROM "attachments" WHERE ("attachments".photo_id = 1)
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1)
  Profile Load (1.7ms)  SELECT "profiles".* FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1
  Context Load (0.2ms)  SELECT "contexts".* FROM "contexts" WHERE "contexts"."id" = 6 ORDER BY "contexts"."display_order" ASC LIMIT 1
  Focus Load (0.1ms)  SELECT "focus".* FROM "focus" WHERE "focus"."id" = 2 ORDER BY "focus"."display_order" ASC LIMIT 1
  LandUse Load (0.2ms)  SELECT * FROM "land_uses" INNER JOIN "land_uses_photos" ON "land_uses".id = "land_uses_photos".land_use_id WHERE ("land_uses_photos".photo_id = 1 ) ORDER BY "land_uses"."display_order" ASC
Rendered photos/_photo_tiles.html.haml (62.2ms)
  SQL (0.2ms)  SELECT COUNT(DISTINCT "photos"."id") FROM "photos" LEFT OUTER JOIN "attachments" ON "attachments"."photo_id" = "photos"."id" LEFT OUTER JOIN "collections" ON "collections"."id" = "photos"."collection_id" WHERE "photos"."collection_id" IS NULL
Rendered shared/_feedback.html.haml (1.5ms)
  Role Load (0.5ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'admin' AND ("roles_users".user_id = 1 ) LIMIT 1
  CACHE (0.0ms)  SELECT "profiles".* FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1
  CACHE (0.0ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'admin' AND ("roles_users".user_id = 1 ) LIMIT 1
Rendered shared/_menu.html.haml (177.6ms)
Rendered shared/_footer.html.haml (2.7ms)
Rendered photos/index.html.haml within layouts/application (897.1ms)
Completed 200 OK in 1158ms (Views: 912.2ms | ActiveRecord: 8.2ms)

Pour la première page d'une recherche (fonctionne correctement):

Started GET "/photos?utf8=%E2%9C%93&search%5Bcontext_id_in%5D%5B%5D=6&search%5Btagged_with%5D=&search%5Btext_contains%5D=&search%5Bnear%5D%5B%5D=&search%5Bnear%5D%5B%5D=100&commit=Search" for 127.0.0.1 at 2011-04-28 12:00:39 -0500
  Processing by PhotosController#index as HTML
  Parameters: {"utf8"=>"✓", "search"=>{"context_id_in"=>["6"], "tagged_with"=>"", "text_contains"=>"", "near"=>["", "100"]}, "commit"=>"Search"}
  SQL (0.4ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

  SQL (0.3ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Context Load (0.4ms)  SELECT "contexts".* FROM "contexts" ORDER BY "contexts"."display_order" ASC
  Focus Load (0.3ms)  SELECT "focus".* FROM "focus" ORDER BY "focus"."display_order" ASC
  LandUse Load (0.4ms)  SELECT "land_uses".* FROM "land_uses" ORDER BY "land_uses"."display_order" ASC
Rendered static/_help-context.html.haml (4.6ms)
Rendered static/_help-focus.html.haml (3.1ms)
Rendered photos/_search.html.haml (161.0ms)
  SQL (0.4ms)  SELECT COUNT(DISTINCT "photos"."id") FROM "photos" LEFT OUTER JOIN "attachments" ON "attachments"."photo_id" = "photos"."id" LEFT OUTER JOIN "collections" ON "collections"."id" = "photos"."collection_id" WHERE "photos"."collection_id" IS NULL AND "photos"."context_id" IN (6) LIMIT 1 OFFSET 0
  Photo Load (0.7ms)  SELECT "photos".* FROM "photos" WHERE "photos"."collection_id" IS NULL AND "photos"."context_id" IN (6) LIMIT 1 OFFSET 0
  Attachment Load (0.2ms)  SELECT "attachments".* FROM "attachments" WHERE ("attachments".photo_id = 1)
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1)
  Profile Load (1.9ms)  SELECT "profiles".* FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1
  Context Load (0.2ms)  SELECT "contexts".* FROM "contexts" WHERE "contexts"."id" = 6 ORDER BY "contexts"."display_order" ASC LIMIT 1
  Focus Load (0.2ms)  SELECT "focus".* FROM "focus" WHERE "focus"."id" = 2 ORDER BY "focus"."display_order" ASC LIMIT 1
  LandUse Load (0.2ms)  SELECT * FROM "land_uses" INNER JOIN "land_uses_photos" ON "land_uses".id = "land_uses_photos".land_use_id WHERE ("land_uses_photos".photo_id = 1 ) ORDER BY "land_uses"."display_order" ASC
Rendered photos/_photo_tiles.html.haml (204.2ms)
  SQL (0.3ms)  SELECT COUNT(DISTINCT "photos"."id") FROM "photos" LEFT OUTER JOIN "attachments" ON "attachments"."photo_id" = "photos"."id" LEFT OUTER JOIN "collections" ON "collections"."id" = "photos"."collection_id" WHERE "photos"."collection_id" IS NULL AND "photos"."context_id" IN (6)
Rendered shared/_feedback.html.haml (1.3ms)
  Role Load (0.4ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'admin' AND ("roles_users".user_id = 1 ) LIMIT 1
  CACHE (0.0ms)  SELECT "profiles".* FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1
  CACHE (0.0ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'admin' AND ("roles_users".user_id = 1 ) LIMIT 1
Rendered shared/_menu.html.haml (35.5ms)
Rendered shared/_footer.html.haml (2.9ms)
Rendered photos/index.html.haml within layouts/application (729.3ms)
Completed 200 OK in 990ms (Views: 728.5ms | ActiveRecord: 7.7ms)

Pour la deuxième page d'une recherche (montre aucun résultat même si elles existent):

Started GET "/photos?commit=Search&page=2&search[context_id_in][]=6&search[near][]=&search[near][]=100&search[tagged_with]=&search[text_contains]=&utf8=%E2%9C%93" for 127.0.0.1 at 2011-04-28 12:01:33 -0500
  Processing by PhotosController#index as HTML
  Parameters: {"commit"=>"Search", "page"=>"2", "search"=>{"context_id_in"=>["6"], "near"=>["", "100"], "tagged_with"=>"", "text_contains"=>""}, "utf8"=>"✓"}
  SQL (0.4ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

  SQL (0.3ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Context Load (0.3ms)  SELECT "contexts".* FROM "contexts" ORDER BY "contexts"."display_order" ASC
  Focus Load (0.3ms)  SELECT "focus".* FROM "focus" ORDER BY "focus"."display_order" ASC
  LandUse Load (0.3ms)  SELECT "land_uses".* FROM "land_uses" ORDER BY "land_uses"."display_order" ASC
Rendered static/_help-context.html.haml (4.9ms)
Rendered static/_help-focus.html.haml (3.4ms)
Rendered photos/_search.html.haml (307.8ms)
  SQL (0.3ms)  SELECT COUNT(DISTINCT "photos"."id") FROM "photos" LEFT OUTER JOIN "attachments" ON "attachments"."photo_id" = "photos"."id" LEFT OUTER JOIN "collections" ON "collections"."id" = "photos"."collection_id" WHERE "photos"."collection_id" IS NULL AND "photos"."context_id" IN (6) LIMIT 1 OFFSET 1
  SQL (0.3ms)  SELECT COUNT(DISTINCT "photos"."id") FROM "photos" LEFT OUTER JOIN "attachments" ON "attachments"."photo_id" = "photos"."id" LEFT OUTER JOIN "collections" ON "collections"."id" = "photos"."collection_id" WHERE "photos"."collection_id" IS NULL AND "photos"."context_id" IN (6)
Rendered shared/_feedback.html.haml (1.5ms)
  Role Load (0.4ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'admin' AND ("roles_users".user_id = 1 ) LIMIT 1
  Profile Load (1.6ms)  SELECT "profiles".* FROM "profiles" WHERE ("profiles".user_id = 1) LIMIT 1
  CACHE (0.0ms)  SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE "roles"."name" = 'admin' AND ("roles_users".user_id = 1 ) LIMIT 1
Rendered shared/_menu.html.haml (209.5ms)
Rendered shared/_footer.html.haml (2.7ms)
Rendered photos/index.html.haml within layouts/application (822.3ms)
Completed 200 OK in 934ms (Views: 823.7ms | ActiveRecord: 5.1ms)

Encore une fois noter que dans les journaux ci-dessus, je l'ai mis à 1 par la pagination page afin que je puisse voir si le paginator montre le bon nombre de résultats, et il est. Par exemple, si je cherche quelque chose que je sais qu'il ya 3 matchs à raison d'une par page paginateur dit il y a trois pages de résultats, mais les pages 2 et 3 sont vides.

Je suis complètement déconcerté, toute aide serait appréciée.

Était-ce utile?

La solution

Essayez d'ajouter votre champ comme méthode de recherche.

class Photo < ActiveRecord::Base
  scope :community, where(:collection_id => nil)
  search_methods :community
end

class PhotosController < ActiveRecord::Base
  def index
    params[:search][:community] => true
    @search = Photo.search(params[:search])
    @photos = @search.page(params[:page]).per(20)
  end
end

Je ne je pas utiliser Kaminari moi-même ne peut donc pas dire à coup sûr, mais je sais que les métarecherche docs disent utiliser search_methods afin d'utiliser des étendues dans les recherches.

Autres conseils

ça marche pour moi avec les dernières gemmes actuelles (meta_search 1.0.4, Kaminari 0.12.4).

Comme l'auteur de bijou Kaminari, je suppose que certaines anciennes versions de Kaminari ne peut fonctionne pas avec meta_search. En fait, je Correction de plusieurs bugs qui peuvent causer ce genre de problème.

Alors, pouvez-vous s'il vous plaît mettre à jour votre bijou Kaminari et essayez à nouveau?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top