Apane101
Apane101

Reputation: 1123

how to show genres through associatin in the song controller?

if you go to www.leapfm.com you'll see each song has a youtube url in parenthesis to the right of it. After gauging feedback I have decided to instead display the genre tags in the parenthesis.

When trying to do this,

I'm getting this error:

Couldn't find Genre without an ID
Extracted source:

def genre_name
@genre = Genre.find(params[:id])
end
def get_last_song

song_controller snippit

def index
      if params[:query].present? 
      @songs = Song.search(params)
      get_last_song
      genre_name
    elsif params[:genre]
      @songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
      get_last_song
      genre_name
    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
      genre_name
    end
  end

  def genre_name
    @genre = Genre.find(params[:id])
  end

index.html.erb (_song partial) snippit

<div class="title">
  <%=link_to image_tag('arrow.gif'), vote_for_song_path(song), :remote => true, :method => :put  if controller.action_name == "index" %>
  <%= link_to song.title, song %><span class="subtext"> (<%= song.genre_name %>)</span>
</div>

song.rb snippit:

class Song < ActiveRecord::Base

acts_as_voteable

  belongs_to :user
  has_many :comments, :dependent => :destroy
  has_many :genre_songs
  has_many :genres, through: :genre_songs

genre.rb

class Genre < ActiveRecord::Base
  has_many :genre_songs, :dependent => :destroy
  has_many :songs, through: :genre_songs

end

schema snippit

 create_table "genre_songs", force: true do |t|
    t.integer  "genre_id"
    t.integer  "song_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "genre_songs", ["genre_id"], name: "index_genre_songs_on_genre_id", using: :btree
  add_index "genre_songs", ["song_id"], name: "index_genre_songs_on_song_id", using: :btree

  create_table "genres", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  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"
  end

Upvotes: 0

Views: 139

Answers (1)

BroiSatse
BroiSatse

Reputation: 44715

You need to get rid off gendre_name method, as it doesn't give you what you want. You've assigned @songs to some list of songs, hence most likely somewhere in your view you have sth like:

<% @songs.each do |song| %>
   # display song row
<% end %>

if so, use sth like below inside this each "loop":

(<%= song.genres.map(&:names).join(', ') %>)

Upvotes: 1

Related Questions