I am trying to set an attribute on an object that I am creating. I feel like this should work:
def create @album = Album.new(params[:album]) @album.user = current_user if @album.save flash[:notice] = 'Album was successfully created for ' + current_user.login + '.' redirect_to albums_url else render :action => 'new' end end
But it seems to ignore the assignment to the user field. Any ideas?
Assuming that your model relationships are set up correctly*, it’s better to do:
—This will correctly populate the
user_idfield for the new album to the ID of the current user.*Something like: