Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6970803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:45:23+00:00 2026-05-27T16:45:23+00:00

So I have a rails app That lets users create lists and put items

  • 0

So I have a rails app That lets users create lists and put items on that list. As a feature a user can put any one item on multiple lists. Example:

List_1
  -item_1
  -item_2

List_2
  -item_3
  -item_2

The relationship side of it all is complete and working fine.

The next step was to let the user arrange the items in their lists. I installed act_as_list and using jQuery sortable got a simple drag and drop working for sorting items. The issue is each item has one position. So if I change item_2’s position to 1 on list_1, item_2’s position is now 1 in every list.

I’m trying to think of a way to store item positions relative to the list. Anyone have any thoughts?

Edit to Add code

List model

class List < ActiveRecord::Base
  belongs_to :user

  has_many :assignments, dependent: :destroy
  has_many :items, through: :assignments, order: 'position'

end

Item model

class Item < ActiveRecord::Base
  belongs_to :user

  has_many :assignments, dependent: :destroy
  has_many :lists, through: :assignments
  acts_as_list scope: :list
end

Assignment model

class Assignment < ActiveRecord::Base
  belongs_to :item
  belongs_to :list

  validates_uniqueness_of :item_id, :scope => :list_id
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:45:24+00:00Added an answer on May 27, 2026 at 4:45 pm

    So an answered my own question with a little time an effort. I’ve provided an example below for anyone who may run into the issue in the future.

    Basically @thedeeno’s answer would have worked fine if I was able to use scopes. In my case the list will be user created and hence scopes are not an option.

    Solution:
    So my solution was adding a position column to my assignments table (the join table for my has many through)

    This way each assignment could now have a list_id and item_id and a position.

    Then in my controller I did the following to populate the @items hash

    @items = @list.items.order(:position)
    

    This gave makes @item has ordered by item positions for their respective list position.

    Now the last step was updating their position using sortable. To update the correct item’s postion I needed sortable to send the list_id along with each order of the id’s. To get the list_id sent with the post I adding a hidden li with and id of the @list like so

    <li id='list_<%=@list.id %> style='display:none'></li>
    

    which meant that my params now looked like this

    {"list"=>["31"], "item"=>["30", "58", "59", "24", "80", "81"]}
    

    Finally using the skills I had learned from http://railscasts.com/episodes/147-sortable-lists I changed my sort action to the following

    def sort
      @items = params[:item]
    
      @items.each_with_index do |id, index|
        assignment = Assignment.where(list_id: params[:list], item_id: id).update_all(['position=?', index+1])
      end
    
    render :nothing => true
    end
    

    That’s it! users can now create any number of lists and item to multiple lists and sort each list independently. Hope this helps someone!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a ruby on rails app that lets users track their workouts.
I have built a ruby on rails app that lets users track their workouts.
I have built a ruby on rails app that lets users track their workouts.
Let's say I have a Rails 3 app that displays videos. The user can
I have a Rails app that uses STI to handle different types of Users,
I have a Rails app that allows users to build up a network structure
I have a Rails web app that shows data to user, however, that data
I have this Rails app that can be spawned (by Passenger) a number of
Have a rails app that is supposed to display a list of products/managers. After
I have a Rails app that sends a PDF to the user. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.