al3xnull
al3xnull

Reputation: 866

Cannot find the child_model (NameError)

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!

Upvotes: 3

Views: 1408

Answers (2)

hoitomt
hoitomt

Reputation: 670

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'

Upvotes: 0

namelessjon
namelessjon

Reputation: 907

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

Upvotes: 6

Related Questions