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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:33:43+00:00 2026-06-01T19:33:43+00:00

I have a has_many through association setup between a song model and an artist

  • 0

I have a has_many through association setup between a song model and an artist model.
My code looks something like this

SongArtistMap Model

class SongArtistMap < ActiveRecord::Base
 belongs_to :song
 belongs_to :artist
end

Artist Model

class Artist < ActiveRecord::Base
 has_many :song_artist_maps
 has_many :songs, :through => :song_artist_maps

 validates_presence_of :name
end

Song Model

class Song < ActiveRecord::Base
  has_many :song_artist_maps
  has_many :artists, :through => :song_artist_maps
  accepts_nested_attributes_for :artists
end

I have a form where a user submits a song and enters in the song title and the song artist.

So when a user submits a song and my Artists table doesn’t already have the artist for the song I want it to create that artist and setup the map in SongArtistMap

If a user submits a song with an artist that is already in the Artists table I just want the SongArtistMap created but the artist not duplicated.

Currently everytime a user submits a song a new artist gets created in my artists table even if the same one already exists and a SongArtistMap is created for that duplicated artist.

Any idea on how to tackle this issue? I feel like rails probably has some easy little trick to fix this already built in. Thanks!

  • 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-06-01T19:33:44+00:00Added an answer on June 1, 2026 at 7:33 pm

    Ok I got this figured out awhile ago and forgot to post. So here’s how I fixed my problem. First of all I realized I didn’t need to have a has_many through relationship.

    What I really needed was a has_and_belongs_to_many relationship. I setup that up and made the table for it.

    Then in my Artists model I added this

    def self.find_or_create_by_name(name)
      k = self.find_by_name(name)
    
      if k.nil?
        k = self.new(:name => name)
      end
    
      return k
    end
    

    And in my Song model I added this

    before_save :get_artists
    def get_artists
      self.artists.map! do |artist|
       Artist.find_or_create_by_name(artist.name)
      end
    end
    

    And that did exactly what I wanted.

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

Sidebar

Related Questions

I have Product model and it has many categories with a has_many :through association
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
I'm trying to setup a simple has_many :through => association. I have 3 models.
I have 3 models set up like this: Trip has_many :workgroups, through: :invitations Workgroup
I have an Organization model that has_many users through affiliations. And, in the form
I have a event model that has many invitations. Invitations are setup through checkboxes
I have a has_many :through association, and I am iterating over the children to
I setup a has_many through association with my models and I am trying to
I have a join model in a HABTM relationship with a through association (details
So I have the basics setup already. This is my User model: # ==

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.