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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:49:47+00:00 2026-05-24T11:49:47+00:00

About an hour ago i asked a question on rails associations: Question on Proper

  • 0

About an hour ago i asked a question on rails associations:
Question on Proper Associations in Rails

The accepted answer from that question got me thinking about relationships more deeply and I’d like to present the SO community with this situation.

My previous question used poet, poem, and printing as the models… For this one let’s use the music industry:

The models are:

  • Artist
  • Album
  • Song
  • Genre

The following statements are to be considered true:

  1. An album can have multiple artists – ie Metallica and Pantera release an xmas album
  2. A song can belong to multiple albums – ie Yellow Submarine is on the original album as well as a number of “Greatest hits” albums from The Beatles
  3. An individual song can also have multiple “featured” artists that differ from the album artist – ie Snoop Dogg owns the album but does a song featuring Harry Connick Jr. Or an even better example is when a DJ releases an album where ALL the songs are by other artists.
  4. Artist, Album, and Song can all be classified under multiple/different Genres – ie Brian Setzer Orchestra is categorized as “Swing”, one of their albums could be “Swing, Rockabilly” and an individual song on that album could be “Jump Blues”.

When digging through this problem, I immediately see that models such as Artist & Genre can be “reused”. We would NOT want to save a artist’s information more than once — so for example in the event we have a song w/ both a main artist and featured artist BOTH artists’ information should live in the DB’s “Artist” table.

Additionally, when reviewing the “featured” artist aspect (statement #2) it seems we have an additional attribute that should be on the association — something like a “featured” flag.

Here is how i think the associations should be setup — and also the apex of my post…. is this right, and how can i make it better?

class Artist < ActiveRecord::Base
  has_and_belongs_to_many :albums
  has_and_belongs_to_many :genres
  has_many :featurings
  has_many :features, :through => :featurings, :conditions => "featured = true"
end

class Album < ActiveRecord::Base
  has_and_belongs_to_many :artists
  has_and_belongs_to_many :songs
  has_many :featurings
  has_many :featured_artists, :through => :featurings, :conditions => "featured = true"
end

class Song < ActiveRecord::Base
  has_and_belongs_to_many :genres
  has_many :artists
  has_many :featurings
  has_many :featured_artists, :through => :featurings, :conditions => "featured = true"
end

class Genre < ActiveRecord::Base
  has_and_belongs_to_many :artists
  has_and_belongs_to_many :songs
end

class Featurings < ActiveRecord::Base
  # the db table for this class should have a "featured" boolean.
  belongs_to :artist
  belongs_to :album
  belongs_to :song
end

As usual, huge thanks to those who take the time to read and give input! it’s much appreciated!

  • 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-24T11:49:48+00:00Added an answer on May 24, 2026 at 11:49 am

    This is more of a discussion than a question, but I’ll attempt to address your concerns. I am not testing any of your code, so these are just my thoughts.

    (Artist) has_any_belongs_to_many :genres
    

    Should you need a separate table for this? The information is already stored through the association between songs and genres. Unless artists can belong to genres despite not having songs in said genres, you should not reproduce this information in another HABTM association.

    Vice versa, the opposite association may be redundant:

    (Genre) has_any_belongs_to_many :artists
    

    As for your design of the featuring aspect, it seems that having a feature with the flag featured set to true is also redundant. However, that is because of the naming, so if I were you I’d rename it to Release instead (songs can be released on multiple albums). If you think of the featured flag as existing between the artist and the song, you should add the flag to the enjoining entity (forgot the term for this).

    However, since it is expressed as a HABTM association, there is no intervening model. Thus, you will have to convert this to use has_many on both Song and Artist, with the intervening model containing belong_to associations as well as a featured flag. This is actually the Release model.

    This (again no testing has been done) cuts down your models to this:

    class Artist < ActiveRecord::Base
      has_and_belongs_to_many :albums
      has_many :releases
      has_many :songs, :through => :releases
      has_many :albums, :through => :releases
      has_many :featured_songs, through => :releases, :conditions => "featured = true"
    end
    
    class Album < ActiveRecord::Base
      has_and_belongs_to_many :artists
      has_many :releases
      has_many :songs, :through => :releases
      has_many :artists, :through => :releases
    end
    
    class Release < ActiveRecord::Base
      belongs_to :artist
      belongs_to :song
      belongs_to :album
      # there should be a featured boolean
    end
    
    class Song < ActiveRecord::Base
      has_and_belongs_to_many :genres
      has_many :releases
      has_many :artists, :through => :releases
      has_many :albums, :through => :releases
      has_many :featured_artists, through => :releases, :conditions => "featured = true"
    end
    
    class Genre < ActiveRecord::Base
      has_and_belongs_to_many :songs
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just asked a question about an hour ago, while waiting for replies, I've
Note: I originally asked this question about an hour ago but only recently realized
Somebody has posted an hour ago or so a question that was about the
I have asked a similar question before and got an convincing answer as well?
In asking a question about reflection I asked: Nice answer. But there is a
I posted up a question about an hour ago asking a regex question about
I just installed nginx and php fastcgi about an hour ago, and after reading
I once worked on a C++ project that took about an hour and a
I have a question, I am searching for about an hour now. A given
I've just (about 1 hour ago) associated an Elastic IP to my instance at

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.