找不到child_model(NameError)

时间:2011-06-14 20:45:06

标签: ruby sinatra datamapper

我无法确定为什么我在这里收到名称错误。我是DataMapper的新手,但尝试关联。任何帮助表示赞赏。

用户:

class User
  include DataMapper::Resource

  property :id,            Serial, :key => true
  property :first_name,    String
  property :last_name,     String
  property :company,       String
  property :city,          String
  property :country,       String
  property :mobile_number, Integer
  property :email_address, String
  property :shahash,       String
  property :isRegistered,  Boolean

  belongs_to :event, :required => true
end

DataMapper.auto_upgrade!

事件:

class Event
  include DataMapper::Resource

  property :id,          Serial, :key => true
  property :name,        String
  property :occuring,    DateTime

  has n, :user
end

DataMapper.auto_upgrade!

2 个答案:

答案 0 :(得分:6)

我认为问题是你在每个模型定义之后调用DataMapper.auto_upgrade!。当您在定义一个模型后调用它时,那里没有子模型。相反,您应该定义和/或要求所有模型,然后执行:

DataMapper.finalize      # set up all relationships properly
                         # and do a basic model sanity check
DataMapper.auto_upgrade! # create database table if it doesn't exist

答案 1 :(得分:0)

在models目录中添加一个init文件,并将所有DataMapper.finalize语句移到它(即从各个模型文件中删除finalize语句)

应用程序/模型/ init.rb

require_relative 'file_name'
require_relative 'another_model_file_name'

DataMapper.finalize

然后在您的应用程序文件中需要init文件

require_relative 'models/init'