0
votes

I'm new to rails and I'm wondering if my intuition about how to set up the following association is correct.

I have Partner Themes that need to have a default Audio Theme associated with them. The Audio Theme then has many songs associated with it. So Audio Themes will have multiple songs and multiple Partner Themes will have the same Audio Theme.

Should I set it up like the following?

Partner Theme: has_one :audio_theme has_many :songs, through: :audio_theme

Audio Theme: has_and_belongs_to_many :partner_themes has_many :songs

Songs: belongs_to :audio_theme has_and_belongs_to_many :partner_themes, through: :audio_theme

Also how should I set up the migrations for all these associations if the models already exist but the associations don't?

Thanks!

1

1 Answers

1
votes

I think you can set it up more simply - like this:

Audio.rb

has_many :PT
has_many :songs

Partner.rb

belongs_to :AT

Song.rb

belongs_to :AT

You don't need to worry about migrations, just put that code into the models and you're set. To access, for example, a song with ID 33, you could use audio.songs.find(33)