0

I Have 4 objects

@ob1 = User.where(:area => "India")
@ob2 = User.where(:area => "USA")
@ob3 = User.where(:area => "UK")
@ob4 = User.where(:area => "China")

Merged above 4 objects in on object

@merged_obj= @obj1+@obj2

Merged object and passing to pagination

@users = @merged_obj.paginate(:page => params[:page], :per_page => 1)

But troughs error

NoMethodError: undefined method `paginate' for #<Array:0x007fc3d65a5338>

Pagination works with below code:

@users = User.where(:area => "India").paginate(:page => params[:page], :per_page => 1)

Gems:-

gem 'will_paginate', '~> 3.1.0'
gem 'will_paginate-bootstrap'
s1mpl3
  • 1,456
  • 1
  • 10
  • 14
khalidh
  • 875
  • 2
  • 12
  • 34
  • Have you tried the solution here yet? The second answer actually got many more votes : http://stackoverflow.com/questions/13187076/paginating-an-array-in-ruby-with-will-paginate – Rockwell Rice Apr 14 '17 at 14:18
  • I want to display first USA users next INDIA next UK.. Using pagination, It showing random – khalidh Apr 14 '17 at 15:00

1 Answers1

1

It looks like it won't work with arrays. Do any of the following instead

@obj = User.where(:area => ["India","USA","UK","China"])

or

@merged_obj = User.where(id: [@ob1.id, @ob2.id, @ob3.id, @ob4.id])

EDIT: responding to the comments if denormalizing the area is not an option do your own pagination on the array

@page = @merged_obj[start, pagesize]
s1mpl3
  • 1,456
  • 1
  • 10
  • 14
  • I want to display first USA users next INDIA next UK.. Using pagination, It showing random – khalidh Apr 14 '17 at 15:01
  • That would be a different question. You will need to create a new model, Area with a sort_order field so you can have the users sorted however you need. – s1mpl3 Apr 14 '17 at 15:38
  • this is also useful to me, but i want sorting also, first display all USA records next India records.... – khalidh Apr 14 '17 at 18:25
  • @merged_obj[start, pagesize] will do it – s1mpl3 Apr 14 '17 at 23:03