2

There are three tables in database. Using these tables, need to show details from these tables in grid/table format on page.

To fetch the details from three tables, I created one view in database. But the issue is how can I implement sorting and pagination on view?

Is any solution for this? For existing grids on pages I used will_paginate

kd12
  • 1,291
  • 1
  • 13
  • 22

1 Answers1

0

A view responds to SELECT queries like a normal table (and possibly to updates, etc. depending on your database server - example MySQL. You should be able to use the standard approach: write a model for that view and a controller for that model

Ryan Bates Railscast website holds an episode on Sortable Table Columns. The tutorial is a bit outdated but the general idea still applies. You can use will_paginate when you read the objects.

smile2day
  • 1,585
  • 1
  • 24
  • 34
  • Any idea about the performance impact of adding pagination to a view? Because a view is no more than a stored procedure? So I imagine if you have a view with e.g. a million records and select with paging only 10 of them, you would still need to calculate the view with a million records... ? – Thomas Stubbe Jul 03 '19 at 14:52
  • Performance should be similar to the performnce of the query which defines the view. Should also very much apply to pagination (achieved through OFFSET and LIMIT on the view). A view is a cached query, the DDL defining a virtual table, not a stored procedure. Some database servers implement materialised (persisted) views. A view uses the same optimisations as the defining query. Good indexes will not require a full scan of the data when you pick 10 from anywhere. A thread discussing performance: https://stackoverflow.com/questions/529259/do-database-views-affect-query-performance – smile2day Jul 05 '19 at 08:06