1

Why am I getting this now? A new user uploaded a song, I went to vote it up and I'm getting the following: (this happened after I created staging branch)

ActiveRecord::RecordNotFound (Couldn't find Song with id=attention-let-me-go-remix):
2013-08-14T06:11:31.740608+00:00 app[web.1]:   app/controllers/songs_controller.rb:13:in `vote_for'

song controller snippit

class SongsController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create ,:edit, :update, :destroy, :vote_for_song]
  before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]

  def extract_video

  @song = Song.find(params[:id])
  @song.YouTubeAddy.extract_video_id

  end

  def vote_for
      @song = Song.find(params[:id])
      current_user.vote_for(@song)
      @song.plusminus = @song.votes_for
      @song.save
      respond_to do |format|
        format.js { render 'update_votes' }
      end
  end

  def vote_against
    @song = Song.find(params[:id])
    current_user.vote_against(@song)
    respond_to do |format|
      format.js { render 'update_votes' }
    end
  end

  def new_songs
    @songs = Song.order("id DESC").paginate(:page => params[:page], :per_page => 15)
    get_last_song
  end


  # GET /Songs
  # GET /Songs.json
  def index
    if params[:query].present? 
      @songs = Song.search(params)
      get_last_song
    elsif params[:genre]
      @songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
      get_last_song
    else      
      @songs = Song.order('id').order('plusminus desc nulls last').paginate(:page => params[:page], :per_page => 15) 
      #@songs = Song.tally.paginate(:page => params[:page], :per_page => 15)
      get_last_song
    end
  end


  def get_last_song
    if params[:page].nil?
      @last_song = 0
    else
      @last_song = 15 * (params[:page].to_i - 1)
    end
  end

  # GET /Songs/1
  # GET /Songs/1.json
  def show
   @comment = Comment.new(song: @song) 
   @video_tag = YouTubeAddy.extract_video_id(@song.url)

  end

  # GET /Songs/new
  def new
    @song = Song.new
  end

  # GET /Songs/1/edit
  def edit
  end

  # POST /Songs
  # POST /Songs.json
  def create
    @song = Song.new(song_params)
    @song.user = current_user
    respond_to do |format|
      if @song.save
        format.html { redirect_to @song, notice: 'Song was successfully created.' }
        format.json { render action: 'show', status: :created, location: @song }
      else
        format.html { render action: 'new' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end

schema snippit

 create_table "songs", force: true do |t|
    t.string   "title"
    t.string   "artist"
    t.text     "url"
    t.string   "track_file_name"
    t.string   "track_content_type"
    t.integer  "track_file_size"
    t.datetime "track_updated_at"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "plusminus"
    t.string   "slug"
  end
maček
  • 76,434
  • 37
  • 167
  • 198
Apane101
  • 1,121
  • 1
  • 14
  • 37
  • 1
    post the full logs for the action in order to see what params exactly you are sending and what params are being processed by controller. – rmagnum2002 Aug 14 '13 at 08:17

3 Answers3

2

You probably need to change it to @song = Song.find_by_url(params[:id])

Benjamin Tan Wei Hao
  • 9,621
  • 3
  • 30
  • 56
1
@song = Song.find_by_slug(params[:id])

or if you want to find your song by id you'll have to use in your link for vote song.id:

  vote_for_song_path(song.id)

now you can use:

 @song = Song.find(params[:id])

in controller.

rmagnum2002
  • 11,341
  • 8
  • 49
  • 86
-1

You can do this too

@song = Song.all.where(:url => params[:id])

that find_by_attribute thing doesn't seemed to work in my case.

dirtydexter
  • 1,063
  • 1
  • 10
  • 17
  • 1
    That's wrong (no offence): It should be `@song = Song.where(:url => params[:id]).first`. – Benjamin Tan Wei Hao Aug 14 '13 at 06:51
  • @BenjaminTan it isin't wrong sir, url is unique so we don need to use `.first` and `Song.all.where` and `Song.where` will lead to the same result one way or another. – dirtydexter Aug 14 '13 at 10:18
  • Song.all returns an Array, not an Active Relation. You cannot call `where` on an array. – Benjamin Tan Wei Hao Aug 14 '13 at 10:22
  • @BenjaminTan it just returns a subset of all the records as Active Relation and all operations can be performed on it. In case of 'all' all the data will be returned in the Active Relation form only, not array. try printing `p Song.all.class' you will get Active Relation only. – dirtydexter Aug 14 '13 at 11:41
  • What Rails version are you using? AFAIK, Rails 3 returns an Array on `#all`. Please look at http://apidock.com/rails/ActiveRecord/Base/find/class carefully. `1.9.3-p0-falcon :001 > Spree::User.all.class => Array` – Benjamin Tan Wei Hao Aug 14 '13 at 14:26
  • @BenjaminTan My apologies, I must have had some confusion, You were Right. Thanks for clearing this up for me. – dirtydexter Aug 16 '13 at 04:16
  • No problem! It took a while for me to understand that too. – Benjamin Tan Wei Hao Aug 16 '13 at 04:16