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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:31:22+00:00 2026-05-20T19:31:22+00:00

I’m trying to figure out the best way to setup my database and models

  • 0

I’m trying to figure out the best way to setup my database and models for the following scenario.

A user can like an infinite number of songs.
A song can be liked once by an infinite number of users.

I have these tables:
songs, users, likes etc… Following RoR conventions.

The table named likes has these foreign keys: user_id, song_id. And also a field named ‘time’ to save a timestamp when the song was liked.

I’m not sure of how to do this, I would like to be able to use code like this in my controllers:

User.find(1).likes.all
This should not return from the likes table, but join the songs table and output all the songs that the user likes.

What are the best practises to achieve this in Ruby on Rails following their conventions?

  • 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-20T19:31:23+00:00Added an answer on May 20, 2026 at 7:31 pm

    Unless you need to act specifically on the likes table data, the model itself is probably not necessary. The relationship is easy:

    class User < ActiveRecord::Base
      has_and_belongs_to_many :songs
    end
    
    class Song < ActiveRecord::Base
      has_and_belongs_to_many :users
    end
    

    This will join through the currently non-existent song_users table. But since you want it to join through likes you can change each one to this:

    has_and_belongs_to_many :songs, :join_table => 'likes'
    

    If you want to be able to call User.find(1).likes and get songs, then change the user’s version to this:

    class User < ActiveRecord::Base
      has_and_belongs_to_many :likes, :join_table => 'likes', :class_name => 'Song'
    end
    

    And you could change the songs version to something like this:

    class Song < ActiveRecord::Base
      has_and_belongs_to_many :liked_by, :join_table => 'likes', :class_name => 'User'
    end
    

    This will give you Song.find(1).liked_by.all and give you the users (You could keep it to users if you wanted using the first version)

    More details on habtm relationships can be found here: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many

    Edit to add

    If you want to act on the join table for whatever reason (you find yourself needing methods specifically on the join), you can include the model by doing it this way:

    class User < ActiveRecord::Base
      has_many :songs, :through => :likes
      has_many :likes
    end
    
    class Like < ActiveRecord::Base
      belongs_to :user
      belongs_to :song
    end
    
    class Song < ActiveRecord::Base
      has_many :users, :through => :likes
      has_many :likes
    end
    

    This will let you do User.find(1).songs.all, but User.find(1).likes.all will give you the join data

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.