【问题标题】:DataMapper Error in Sinatra - NameError: Cannot find the child_modelSinatra 中的 DataMapper 错误 - NameError: 找不到 child_model
【发布时间】:2012-12-29 23:28:28
【问题描述】:

使用 Sinatra 和 DataMapper。这是我第一次尝试使用两个以上的模型类。不确定是什么导致了错误。谢谢。

错误:

NameError: Cannot find the child_model ContactNote for Contact in contact_notes

模型:

class Contact
    include DataMapper::Resource
    property :id, Serial
    property :fullname, Text, :required => true
    property :email, Text
    property :phone, Text
    has n, :contact_notes
    has n, :projects
end

class Contact_Note
    include DataMapper::Resource
    property :id, Serial
    property :contact_content, Text, :required => true
    property :created_at, DateTime
    property :updated_at, DateTime
    belongs_to :contact
end

class Project
    include DataMapper::Resource
    property :id, Serial
    property :project_name, Text, :required => true
    property :created_at, DateTime
    property :updated_at, DateTime
    belongs_to :contact
    has n, :project_notes
end


class Project_Note
    include DataMapper::Resource
    property :id, Serial
    property :project_content, Text, :required => true
    property :created_at, DateTime
    property :updated_at, DateTime
    belongs_to :project
end

【问题讨论】:

    标签: ruby sinatra datamapper ruby-datamapper


    【解决方案1】:

    Datamapper 根据 ruby​​ 约定对类名做出预期。它希望您的联系笔记属于ContactNote 类,而您已将其命名为Contact_Note,因此它找不到ContactNote 的错误。

    【讨论】:

    • 谢谢。这行得通。我注意到,即使我将类更改为 ContactNote,我也必须将关联属性保留为:contact_notes。如果我删除下划线,我会收到类似的错误。这是另一个数据映射器约定吗?
    • 这是一个通用的 ruby​​ 约定。
    猜你喜欢
    • 2011-09-15
    • 2011-09-29
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2011-02-25
    • 2011-11-03
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多