Find_or_initialize_by and Nested Resources

Hey guys,

Lets say I have the following nested resources:

map.resource artists do |artists|
map.resource songs
end

To create a new song, the url would be:

/artists/:artist_id/songs/:song_id

However, lets say that an artist doesn’t exist yet. I only want to
create an artist if a new song by the artist is successfully saved. I
am retrieving the artist using something like:

@artist = find_or_intialize_by_country_id(@country)

Since the artist doesn’t exist yet, there will be no ‘id’ attribute
yet. However, I have to pass in an id to the url which creates the
song, ie:

new_song_path(@artist)

This will obviously fail since @artist.id is nil. I can use
find_or_create_by instead, but then if the user cancels creating a
song, I will be left with an extra row in the artist table that I
don’t need.

Any ideas?

Shane V.
http://shanesbrain.net