开发者

Cannot find the child_model (NameError)

开发者 https://www.devze.com 2023-03-13 04:41 出处:网络
I am unable to determine why I am getting a name error here. I\'m new to DataMapper, but trying to associations down. Any help is appreciated.

I am unable to determine why I am getting a name error here. I'm new to DataMapper, but trying to associations down. Any help is appreciated.

User:

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!

Event:

class Event
  include DataMapper::Resource

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

  has n, :user
end

DataMapper.auto_upgrade!


I think the problem is you're calling DataMapper.auto_upgrade! after each model definition. When you call it after just defining one model, there's no child model there. Instead, you should define and/or require all your models and then do:

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


Add an init file in your models directory and move all of your your DataMapper.finalize statements to it (i.e. remove the finalize statement from your individual model files)

app/models/init.rb

require_relative 'file_name'
require_relative 'another_model_file_name'

DataMapper.finalize

Then in your application file require the init file

require_relative 'models/init'
0

精彩评论

暂无评论...
验证码 换一张
取 消