How to implement custom row ordering

Hi there!
   I am still new to ruby/rails and there is a problem that looks rather
trivial yet I am wondering what the neatest solution may be.
   I'd like the rows to be ordered "by hand", i.e. I'd like to allow the
user to indicate the ordering of the rows (e.g. this goes after that,
this goes first and so forth). As it happens, I am not a very
experienced web developer and the only solution that comes to my mind is
to use an integer column (e.g. named `position`) and when listing the
rows to order by that column. However, there is the problem that this
realization would require table locking: for instance if the user moves
the 7th item to the beginning, the `position` indexes of all the first
six rows would have to be modified (incremented by one to be precise).
   I'd be very grateful I anyone could propose some nice and very
"rails-like" solution to my (I'd say rather) ordinary problem.

   Thank you very much!

Hi there!

I am still new to ruby/rails and there is a problem that looks rather

trivial yet I am wondering what the neatest solution may be.

I’d like the rows to be ordered “by hand”, i.e. I’d like to allow the

user to indicate the ordering of the rows (e.g. this goes after that,

this goes first and so forth). As it happens, I am not a very

experienced web developer and the only solution that comes to my mind is

to use an integer column (e.g. named position) and when listing the

rows to order by that column. However, there is the problem that this

realization would require table locking: for instance if the user moves

the 7th item to the beginning, the position indexes of all the first

six rows would have to be modified (incremented by one to be precise).

I’d be very grateful I anyone could propose some nice and very

“rails-like” solution to my (I’d say rather) ordinary problem.

Thank you very much!

Posted via http://www.ruby-forum.com/.

You received this message because you are subscribed to the Google Groups “Ruby on Rails: Talk” group.

To post to this group, send email to rubyonrails-talk@googlegroups.com.

To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Oops… pesky phone!

Google for “acts_as_list” to sort this for you.

Michael Pavling wrote:

Oops... pesky phone!

Google for "acts_as_list" to sort this for you.

Thank you! I am googling it straight away!

Michael Pavling wrote:

Oops... pesky phone!

Google for "acts_as_list" to sort this for you.

It seems you are quite correct - it is exactly what I needed. I am very
grateful for this accurate and immediate response! Long live the ruby
lang!

Well. I am having trouble understanding how acts_as_list works. I do not
quite comprehend what the table schema for the example given by DHH must
be:

[code]

  class TodoList < ActiveRecord::Base
    has_many :todo_items, :order => "position"
  end

  class TodoItem < ActiveRecord::Base
    belongs_to :todo_list
    acts_as_list :scope => :todo_list
  end

[/code]

More precisely I wonder where should the `position` column go? To me it
looks like it should go with the table having the foreign keys, i.e.
`todo_items` table. Then it would seem that there would be no need for a
`todo_lists` table, right? So it would be all about the todo_items
table.

Thank you!

More precisely I wonder where should the `position` column go? To me it
looks like it should go with the table having the foreign keys, i.e.
`todo_items` table.

Correct - it is the items that are ordered with respect to each other.

Then it would seem that there would be no need for a
`todo_lists` table, right? So it would be all about the todo_items
table.

You could conceivably do that (and have the todo_list_id just being an
identifier for splitting the items into different lists rather than a
foreign key), but in most use cases you'd want to track some per list
information (for example who created it, the name of the list etc.)

Fred

Hi,

What you need is to watch a Railscast: http://media.railscasts.com/videos/147_sortable_lists.mov

It will explain all you need and save you hours of work.

Thank you Fred! Most instructive.

Riccardo Tacconi wrote:

Hi,

What you need is to watch a Railscast:
http://media.railscasts.com/videos/147_sortable_lists.mov

It will explain all you need and save you hours of work.

Thank you Riccardo! Indeed I have watched this railscast. It is quite
practical. However, there was nothing much said about using
`acts_as_list`.

There is not a great deal more to say, what else do you want to know?

Colin

Colin Law wrote:

There is not a great deal more to say, what else do you want to know?

Colin

  It's scope that is bothering me. Firstly we can have our rows grouped
by a foreign key (which seems trivial enough). However, what happens if
it is a polymorphic reference, i.e. we need to use two columns to select
a unique row?

  It is very kind of all those people bothering to answer such trivial
questions as mine. Thank you!

Scope can be a string in which case you can include more stuff or you
can override the scope_condition method - have a nose around in the
source code for acts as list

Fred

I will! Thanks Fred.

Well. I am having trouble understanding how acts_as_list works. I do not
quite comprehend what the table schema for the example given by DHH must
be:

[code]

class TodoList < ActiveRecord::Base
has_many :todo_items, :order => "position"
end

class TodoItem < ActiveRecord::Base
belongs_to :todo_list
acts_as_list :scope => :todo_list
end

[/code]

More precisely I wonder where should the `position` column go? To me it
looks like it should go with the table having the foreign keys, i.e.
`todo_items` table. Then it would seem that there would be no need for a
`todo_lists` table, right? So it would be all about the todo_items
table.

Yes; the position column goes on the table you want ordered.
The purpose of the todo_list table is to allow there to be more than
one list of items (if you want one). The "scope" field is a way of
identifying what separates the lists in the items table.

So if every user needs a list, then you can scope by user; if you're
only having one list of the items, then there's no need for a scope -
just position.

It's scope that is bothering me. Firstly we can have our rows grouped
by a foreign key (which seems trivial enough). However, what happens if
it is a polymorphic reference, i.e. we need to use two columns to select
a unique row?

Right.... your posting is suffering a little "feature creep" at the
moment. If you post what the limits are of the real problem you have,
then people can suggest solutions. If you have a complicated data
structure, then post some of the model code so we can all get our
heads around it.