I have a situation where I have three models, Album, Artists and
Songs:
class Album < ActiveRecord::Base
belongs_to :song
end
class Artist < ActiveRecord::Base
belongs_to :song
end
class Song < ActiveRecord::Base
belongs_to :artist
end
So, an album can have songs that have artists.
The problem is that on the Album create form, the user is allowed to
enter all three values. So, the user may enter an existing album that
has a new song by a new artist. Or they have enter a new album that
has an existing song by an existing artist.
I can’t figure out how to manage this create in the controller. So,
far I have:
def create
@artist = Artist.find_or_create_by_name(params[:artists][:name])
@song = Song.find_by_name(params[:songs][:name])
if @song.nil?
@song = Song.new(:name => params[:songs][:name], :artist_id
=> @artist.id)
if !@song.save
flash[:error] = “Couldn’t create song”
redirect_to :action => ‘new’
end
end
@album = Album.new()
@album.item_id = @item.id
@album.song_id = @song.id
if @album.save
flash[:notice] = 'Album was successfully created.'
redirect_to :action => 'show', :id => @album.id
else
render :action => 'new'
end
end
This seems to work, but it feels kludgy and I’m not sure if there is
another, more Rails appropriate way to do it. Any suggestions?