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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:53:57+00:00 2026-05-31T23:53:57+00:00

I am trying to setup a form that takes in some song information. Right

  • 0

I am trying to setup a form that takes in some song information. Right now the song title and the song artist.

Here is some of my code so far.

Song Model *EDIT

class Song < ActiveRecord::Base

  has_one :song_artist_map
  has_one :artist, :through => :song_artist_map
  accepts_nested_attributes_for :artist

end

Artist Model

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

SongArtistMap Model

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

Songs Controller

def new 
  @song = Song.new
  @song.artist.build
end

And inside my form I added this code

<% f.fields_for :artist do |a| %>
    <li><%= a.label :name %></li>
    <li><%= a.text_field :name %></li>
<% end %>

Right now nothing shows up in my form for artists.

So I need a way to be able to add an Artist from the form when a song is add and then make the mapping or just make the mapping if the artist already exists in my database.

I know I’m doing something very wrong here, can anyone please help? 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-05-31T23:53:58+00:00Added an answer on May 31, 2026 at 11:53 pm

    In your Song model, it should be has_many :artists?

    If you do

    "artist".pluralize
     => "artists" 
    

    That’s what Rails uses for the auto-lookups of a few things, especially with has_many relationships, so it could be the source of your problems.

    EDIT

    In that case, the problem is in your controller. Instead of @song.artist.build, you should have @song.build_artist.

    With a has_many relationship, Rails uses an object that allows you to instantiate new ones, with a has_one, it just returns it (which can be nil.).

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

Sidebar

Related Questions

I'm trying to setup an email form to send feedback from my website, here
I'm trying to setup a html form that will take a file from a
Here's my problem: I'm trying to set up a simple mobile contact form with
Trying to setup some validation on the add to cart button for the dropdown
I have been trying to setup git for our web development team unsuccessfully. Some
I'm trying to set up a form with multiple dropdown lists, that will allow
I'm trying to setup Forms Authentication in an asp.net mvc 2 application that will
I'm building an application using jsp's, servlets, and all that fun stuff. Right now,
I've got an asp.net page that has c# code-behind that does some stuff in
I'm trying to setup a servlet so that users on Android handsets/tablets (using the

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.