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 4573230
In Process

The Archive Base Latest Questions

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

Im building an app where Users have some sort of wishlist a User can

  • 0

Im building an app where Users have some sort of wishlist

a User can have only one wishlist, and can add existing Items to that wishlist
however the Items belong to other Users on the site

I need to be able to access the wishlist items through current_user.wishlist.items (im using Devise so current_user is available)

i though of adding a wishlist_id column to the items table, but that wouldnt work since items can belong to multiple wishlists.

this seems simple but im having a hard time visualizing the relationship or the migration im supposed to generate

  • 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-21T19:45:24+00:00Added an answer on May 21, 2026 at 7:45 pm
    class User < ActiveRecord::Base
    
      has_one :wishlist # or belongs_to :wishlist, it depends which you prefer
    
    end
    
    class Wishlist < ActiveRecord::Base
    
      belongs_to :user
      has_and_belongs_to_many :items
    
    end
    

    And of course:

    ./script/rails generate migration create_item_wishlists wishlist_id:integer item_id:integer
    

    to create join table between items and wishlists.

    UPDATE: To answer “frank blizzard” question in comment:

    Let’s say you have the same structure as in my answer (just change Item to Product or other model name), with HABTM relationship you just need to add new “item” to collection of “items”, and then save wishlist:

    @user.wishlist.items << item
    @user.wishlist.save
    

    You can make it method in user:

    class User
      def add_to_wishlist(item)
        wishlist.items << item
      end
    end
    

    If you want to remove or modify collection of “items”, just use any Ruby method from Array and then save wishlist, which will check differences for you and save only changes.

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

Sidebar

Related Questions

i'm building an app that will display some newsletters to the users. the newsletters
I'm building a simple app, a sort of project/tasks manager where I can have
I am building an app in which the users can have different roles (like
Now, I'm building an application to learn english. In my app, when user have
I am building a PhoneGap/Cordova app and have a simple code that uses http://ricostacruz.com/jquery.transit/
I'm building an app that allows users to view pictures off a website. I
I'm building an app that allows users to snap pictures using the iPhone camera,
Im building an app which uses Sqlite DB. Users can enter their information into
I have this Xcode project that I use for building an App Store app
I'm building an app in Rails that allows users to upload images to their

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.