【问题标题】:Cannot find the child_model - rescue in child_model找不到 child_model - 在 child_model 中救援
【发布时间】:2011-09-29 21:28:26
【问题描述】:

以下型号出现以下错误。我不知道为什么。非常感谢任何帮助。

Cannot find the child_model Document for Event in documents /usr/local/rvm/gems/ruby-1.9.2-p180/gems/dm-core-1.1.0/lib/dm-core/associations/relationship.rb:173:in 'rescue in child_model'

文档模型:

# encoding: utf-8

class Document
  include DataMapper::Resource
  include Paperclip::Resource

  timestamps :at

  property :id,                 Serial
  property :name,               String, :required => true
  property :doc_file_name,      String, :length => 255
  property :doc_content_type,   String, :length => 255
  property :doc_file_size,      Integer
  property :doc_updated_at,      DateTime
  has_attached_file :doc, :url => "/doc/:attachment/:id/:basename.:extension", :path => "#{settings.root}/public/doc/:attachment/:id/:basename.:extension"

  belongs_to :event

  validates_attachment_presence :doc
end

事件模型:

# encoding: utf-8
class Event
  include DataMapper::Resource
  timestamps :at

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

  has n, :documents
  has n, :contacts
  has n, :users
  has n, :agendas
  has n, :questionnaires
  has n, :entries, :required => false
end

【问题讨论】:

    标签: ruby sinatra datamapper


    【解决方案1】:

    你在哪里打电话给DataMapper.finalize/auto_upgrade?听起来你有某种鸡和蛋的问题。在调用 finalize + auto_upgrade 之前,您需要将所有模型放在一个位置。

    【讨论】:

    • 目前Dir.glob(File.dirname(__FILE__) + '/models/*') {|file| require file} / DataMapper.finalize / # DataMapper.auto_migrate! 虽然,我需要 DataMapper.setup 之前的模型,如果这有影响的话。经过一番胡闹,我想出了其中的一部分,但它包括删除 Dir glob 并用单独的 require 语句替换它。也许处理 dir glob 加载文件的顺序?
    • 在需要绝对路径时必须小心,因为如果在其他地方使用相对路径,您将多次需要同一个文件。这可能会导致问题。 IE。 require "models/foo" + require "./models/foo" 将需要 foo 两次。确保你知道你在哪里需要什么。除此之外,没有足够的信息可以多说。希望你让它工作,如果不继续简化你的代码和设置直到它工作,然后追溯你的步骤并分析导致错误的原因。
    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多