Ben Downey
Ben Downey

Reputation: 2665

Rails DB not seeding. Am suspicious of my syntax

I'm trying to seed my database with some fake data, but it's not working as planned. The app creates playlists. I think I've got it modelled correctly, but when I try to seed the database, I get this error:

rake aborted!
uninitialized constant Playlist::PlaylistSong

When I run a trace, I find that it doesn't like the code below (specifically the second to last line):

my_playlist = Playlist.create!(:name => "my_playlist")
10.times do |n|
  my_playlist.songs.create!(:name => "song #{n+1}")
end

My models are set up so that a playlist has many songs through a table called "playlist_songs" and songs have many playlists through the same table.

Here are my models.

class Playlist < ActiveRecord::Base
  attr_accessible :name

  has_many :playlist_songs
  has_many :songs, :through => :playlist_songs
 end

class Song < ActiveRecord::Base
  attr_accessible :name

  has_many :playlist_songs
  has_many :playlists, :through => :playlist_songs
end

class PlaylistSongs < ActiveRecord::Base
  belongs_to :song
  belongs_to :playlist
end

My theory is that the code "my_playlist.songs" is actually a collection and that's why the create method is failing. But I'm not sure. Am I right? If so, what's the syntax I should be using?

Ben

Upvotes: 0

Views: 83

Answers (1)

Jeff Dickey
Jeff Dickey

Reputation: 5034

Your model name is incorrect. PlaylistSongs should be PlaylistSong

Upvotes: 3

Related Questions