kchang
kchang

Reputation: 91

RubyOnRails - How to Change the Order in which ActiveRecords are Paginated

I'm building a note-taking app. We have a "note" feature that displays a user's notes in the order they were created. (latest towards the bottom). There's a button that allows the user to duplicate the note; after duplicating, the note is added to the bottom. How do I have the note added directly beneath?

For example:

Before: note 1 note 2 <-- User clicks "duplicate" button here note 3

Actual Result: note 1 note 2 note 3 note 2 (copy)

Desired Result: note 1 note 2 note 2 (copy) note 3

Some of the ideas I had were to add a db column that specifies the order in which the items would be displayed, and then paginating based on that column. Is there a more straightforward way of solving this problem? Thanks!

Upvotes: 0

Views: 35

Answers (1)

Joel Friedlaender
Joel Friedlaender

Reputation: 2191

There are only two ways I can think of:

  1. Create a column called "order" as you mentioned, so you can programmatically set the order.

  2. When you create the copy, hack the created_at to copy that the same as the previous one you copied. You are sorting by created_at already so it will end up next to it.

I recommend option 1 as you will make it so the created_at is not trustworthy if you choose option 2.

Upvotes: 1

Related Questions