建模问题

时间:2010-10-23 21:55:57

标签: ruby-on-rails ruby-on-rails-3

我有以下两种模式:

class Project < ActiveRecord::Base
  has_many :photoalbums
end

class PhotoAlbum < ActiveRecord::Base
  belongs_to :space
end

routes.rb中:

resources :projects do
  resources :photo_albums
end

我在Controller中尝试做的是获取所有项目的photoalbums列表:

class PhotoAlbumsController < ApplicationController
  def index
    @project = Project.find(params[:project_id])
    @photoalbums = @project.photoalbums.all
  end
end

但我收到以下错误?

uninitialized constant Project::Photoalbum

2 个答案:

答案 0 :(得分:2)

将所有photoalbum替换为photo_album

Rails很聪明,可以将photo_album映射到PhotoAlbum(请注意两个上限)。它仅在字符串的开头或下划线之后大写。如您所见,photoalbum对应Photoalbum(一个上限),在您的应用中不存在。

答案 1 :(得分:1)

尝试has_many :photo_albums,然后@photo_albums = @project.photo_albums