从数据库中删除多个连接记录时未初始化的常量错误

时间:2014-01-31 23:23:18

标签: ruby-on-rails ruby activerecord

我有以下数据库。用户注册到站点后,将为其创建新的GalleryPhoto。他只能有一张GalleryPhoto,可以存储多张照片。

这些是模特:

class User < ActiveRecord::Base
  has_one :gallery_photos, :dependent => :delete
  has_many :photos, :through => :gallery_photos

  after_create :setup_gallery

  def setup_gallery
    GalleryPhoto.create(userId: self.id, name: self.email)
   end
end

class GalleryPhoto < ActiveRecord::Base
  belongs_to :user
  has_many :photos, :dependent => :delete_all
end

class Photo < ActiveRecord::Base
  belongs_to :gallery_photo
end

我想删除用户后:

def destroy
  User.find(params[:id]).destroy
end 

我在该行收到以下错误:

NameError in UsersController#destroy
uninitialized constant User::GalleryPhotos

在用户注册后成功创建了图库,他可以向其添加图像。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

修改此行

has_one :gallery_photos, :dependent => :delete
has_many :photos, :through => :gallery_photos

has_one :gallery_photo, :dependent => :delete
has_many :photos, :through => :gallery_photo