2011-06-14 18 views
3

我無法確定爲什麼在這裏出現名稱錯誤。我是DataMapper的新手,但試圖關聯。任何幫助表示讚賞。找不到child_model(NameError)

用戶:

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! 
+0

你應該使用'has n,:users',複數形式。 – ujifgc 2011-06-15 07:10:19

+0

使用'has n,:users'和'belongs_to:event'時仍然存在問題 我錯過了其他東西? – al3xnull 2011-06-15 13:06:40

回答

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 
+0

我把我的模型分解爲/ models下的單獨文件。在我需要所有模型之後,是否應該調用'DataMapper.finalize/DataMapper.auto_upgrade!'? (對不起,仍然習慣於ORM和Ruby) – al3xnull 2011-06-15 14:42:41

+0

是的,在你需要所有模型後打電話給他們。 – namelessjon 2011-06-15 14:46:36

+0

看起來像那樣=]謝謝無名。 – al3xnull 2011-06-15 15:01:59

0

添加init文件在您的車型目錄,並將所有你對你的DataMapper.finalize聲明它(即從刪除的finalize聲明您的個性化模型文件)

應用程序/模型/ init.rb

require_relative 'file_name' 
require_relative 'another_model_file_name' 

DataMapper.finalize 

然後在您的應用程序文件要求init文件

require_relative 'models/init'