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

  • Home
  • SEARCH
  • 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 6806497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:43:05+00:00 2026-05-26T19:43:05+00:00

I’m having some difficulty creating nested objects between a one-to-many relationship. My models are

  • 0

I’m having some difficulty creating nested objects between a one-to-many relationship. My models are Artist and Song, an artist can have many songs and a song must belong to an artist.

Where I’m having issues is creating artists and songs. For the user experience, I want users to create artist’s songs at the same time they are creating the artist, so my Artist controller looks like so:

def new
    @artist = Artist.new
    @artist.songs.build
  end

  def create
    @artist = Artist.new(params[:artist])

    if @artist.save
      redirect_to root_path
    else
      render :action => 'new'
    end
  end

and then the form:

<div>
    <%= f.label :name, "Artist" %>
    <%= f.text_field :name %>
  </div>  

  <%= f.fields_for :songs do |song| %>
  <div>
    <%= song.label :title, "Song" %>
    <%= song.text_field :title %>
  </div>
  <% end %>

On my Artist model, I’m currently validating uniqueness of the name, and I want to continue to do so. What I’m trying to figure out, is to find a way that if the user goes to this form, an artist with that name already exist, to ignore saving the artist, but instead to something like this:

@artist = Artist.find_by_name(params[:artist_name])
 @song = @artist.songs.new

Where the new song is created for the exist artist. I can do this now by adding a link, and using my Song controller/views. I’m confused at how I could do this without a user input, and simply build the songs.

Should I be using Javascript to submit a post request if the artist name exists with the song information? I could also add logic to the controller, but don’t know how to add conditions based off validation in the controller, this also smells pretty bad to me. Does anyone have any experience with a similar situation? Appreciate the help.

  • 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-26T19:43:05+00:00Added an answer on May 26, 2026 at 7:43 pm

    You’re wielding a fairly scary sword, here. When validating by an artist name, can you be sure that the song might not be for a different artist of the same name? Because if your project is broad, inevitably you’re going to cross-wire data.

    What I would recommend is to not check this after the fact — but, rather, when users are typing in an artist’s name, to use a jQuery autocomplete plugin like http://jqueryui.com/demos/autocomplete/ so that a user can choose that artist if they are already there.

    If the person types in an artist’s name and that artist does exist already (using find_by_name is fine), then I’d redirect them to an intermediary page with a notice that says: “There is already an artist with that name. Is this the artist you’re looking for?” with some basic info from the artist found in your database. Then you can have 2 links: one that says “Yes” and assists in the create, and one that says “No, create a new artist” and goes from there.

    You should be able to do all of this with rails and not need any ajax posting, from my point of view. Make sure you’re watching the params being passed from page to page. using redirect or render changes that.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

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.