Forum: Ruby on Rails How to reorder rows in a table?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7595ff5e64d9a309c29932d106959973?d=identicon&s=25 Human Dunnil (Guest)
on 2006-02-09 02:35
(Received via mailing list)
Hi,
I have a link table with a location field, here is the code I wrote to
reorder the rows

# move_up action in links controller
def move_up
    current_link = Link.find(params[:id])
    current_link.location -= 1
    current_link.save

    same_link = Link.find_by_location(current_link.location)
    same_link.location += 1
    same_link.save

    redirect_to :action => 'list'
end

But it does not work always, can't figure it out.
What may be the problem?
Is there a better way to do this?

Thanks
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2006-02-09 02:56
(Received via mailing list)
When you change current_link.location in this way, you are potentially
creating a situation where two records have the same location. Then you
search by the same value, so you'll find one of the two with that
location.
If you're unlucky, you'll find the one you just changed, and will just
be
setting it back to its original value again.

I suggest looking at AWDR, p. 254, which describes acts_as_list and
position. Just another reason why I love Rails!

///ark


From: "Human Dunnil" <h.dunnil@gmail.com>


Hi,
I have a link table with a location field, here is the code I wrote to
reorder the rows

# move_up action in links controller
def move_up
    current_link = Link.find(params[:id])
    current_link.location -= 1
    current_link.save

    same_link = Link.find_by_location(current_link.location)
    same_link.location += 1
    same_link.save

    redirect_to :action => 'list'
end

But it does not work always, can't figure it out.
What may be the problem?
Is there a better way to do this?

Thanks
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
This topic is locked and can not be replied to.