M
M
madcore2015-06-05 10:38:52
Ruby on Rails
madcore, 2015-06-05 10:38:52

Communication between models?

There are two models Album, Song
How to do what would when you navigate through Album - give a list of Song to it.
album.rb(models)

class Album < ActiveRecord::Base
  has_many :songs
end

song.rb(models)
class Song < ActiveRecord::Base
  belongs_to :album
end

As I understand it, you need to look towards the show action in the Album controller

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
madcore, 2015-06-05
@madcore


The next step is described here.
You need to add to the album controller

def show
@album = Album.find(params[:id]) 
@songs = @album.songs
end

and in show views
<% @songs.each do |song| %>
<%= link_to song.name, song %>
<% end %>

O
OnYourLips, 2015-06-05
@OnYourLips

You understand correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question