Shantanu Bhalerao
Shantanu Bhalerao

Reputation: 153

Will_paginate behavior for deleted posts - displaying correct number of pages

Background:

I made a new rails 3 app with User model and the model has paperclip attachments (Picture model). I posted the first 15 Picture records (attachments) that I submitted as a test for checking out functionality. I do the following in the controller to display all pictures except first 15 test images:

@pictures=Picture.order('pictures.id DESC').limit(Picture.last.id-15).paginate(:page => params[:page], :per_page => 15)

Though I don't display the first 15 records, will_paginate still shows number of pages assuming the first 15 records were still displayed in the index view (since will_paginate takes this from the Picture model)

What I want to do: Show correct number of pages using will_paginate. Delete the test picture records (first 15 records)

Questions: I could use Picture.destroy(:id) to remove the first 15 Picture records.

  1. After removing the records this way, would will_paginate still show pages assuming those records still existed?

  2. Would will_paginate throw an error saying that Picture with :id =>1 does not exist?

  3. How do I delete the Picture records and start again from :id =>1 ? (though this might not be essential if

Thanks

Upvotes: 0

Views: 342

Answers (1)

lloydpick
lloydpick

Reputation: 1649

Generally when you're testing functionality you should be using a different database to your production environment so that you don't need to mess around with trying to offset ID's or delete after the fact.

To answer your questions though...

  1. No, as they'll be destroyed in the database, unless your using something like is_paranoid they won't exist any more.
  2. will_paginate won't care if the ID's are in sequence or not, it only cares about the total number of rows
  3. Picture.each(&:destroy) will delete all the photos, to reset the auto_increment (a database peice of functionality) you'll need to run something like this...

ALTER SEQUENCE your_sequence_name RESTART WITH 1;

Upvotes: 1

Related Questions