I am working with DataMapper and trying to use associations between Project and Task models. I have models in separate project.rb and task.rb files. When I try to connect them with each other, I get the following error:
Cannot find the parent_model Project for Task in project (NameError)
I understand that this is caused by project.rb requiring task.rb and vice versa, since the association works fine if I just include it in one of the files. Here is the code:
project.rb
require 'dmconfig' require 'task' class Project include DataMapper::Resource property :id, Serial has n, :tasks end DataMapper.auto_upgrade! DataMapper.finalize
task.rb
require 'dmconfig' require 'project' class Task include DataMapper::Resource property :id, Serial belongs_to :project end DataMapper.auto_upgrade! DataMapper.finalize
dmconfig.rb
require 'rubygems' require 'dm-core' require 'dm-migrations' DataMapper::Logger.new($stdout, :debug) DataMapper.setup(:default, 'sqlite://' + Dir.pwd + '/taskmanager.db')
If I remove the association from one of the files, it works fine in at least one direction:
require 'dmconfig' class Project include DataMapper::Resource property :id, Serial end DataMapper.auto_upgrade! DataMapper.finalize
If I want the association to work in both directions, is this the only sensible solution to just put both classes in the same file? Or is there a way I can separate them and still manage?
source share