以下是我的 sails 模型,每当我尝试 save() 时,我都会收到以下错误
[Error: Unknown rule: address]
曾经有与 mongoose 相似的模型,没有任何问题,有人可以看到我的模型中有任何错误吗?
谢谢!
/**
* Users.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
module.exports = {
connection: 'MongoServer',
attributes: {
AcctSessionId: {type: 'string', required: false, default: ''},
UserName: {type: 'string', required: true, unique: true},
UserPassword: {type: 'string', required: true},
AcctStartDelay: {type: 'string', required: false, default: ''},
AcctStopDelay:{type: 'string', required: false, default: ''},
AcctPlan: {type: 'string', required: false, default: ''},
AcctResellerID: {type: 'string', required: false, default: ''},
AcctPrice: {type: 'integer', required: false, default: ''},
AcctCurrency: {type: 'string', required: false, default: 'LBP'},
AcctEnabled: {type: 'boolean', required: false, default: true},
AcctExpiry: {type: 'boolean', required: true},
meta: {
address: {type: 'string', required: false, default: ''},
city: {type: 'string', required: false, default: ''},
mobile: {type: 'integer', required: true, default: ''},
email: {type: 'string', required: false, default: ''},
name: {type: 'string', required: true, default: ''}
}
}
};
最佳答案
您不能像使用 Sails/Waterline ORM 那样嵌套数据。问题出在 meta : {} 位。因此,就 sails 而言,您已经定义了一个名为 meta 的属性,并且正在尝试应用一个名为 的规则(例如 等,这些都不是有效的规则。type : 'string')地址、城市、手机、电子邮件
我建议您创建一个名为address 或userMeta 或类似名称的新模型,然后输入address、city、mobile、email等等。然后将它们关联起来,或者,干脆去掉 meta : {} 包装器,就这样......
/**
* Users.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
module.exports = {
connection: 'MongoServer',
attributes: {
AcctSessionId: { type: 'string', required: false, default: '' },
UserName: { type: 'string', required: true, unique: true },
UserPassword: { type: 'string', required: true },
AcctStartDelay: { type: 'string', required: false, default: '' },
AcctStopDelay: { type: 'string', required: false, default: '' },
AcctPlan: { type: 'string', required: false, default: '' },
AcctResellerID: { type: 'string', required: false, default: '' },
AcctPrice: { type: 'integer', required: false, default: '' },
AcctCurrency: { type: 'string', required: false, default: 'LBP' },
AcctEnabled: { type: 'boolean', required: false, default: true },
AcctExpiry: { type: 'boolean', required: true },
address: { type: 'string', required: false, default: '' },
city: { type: 'string', required: false, default: '' },
mobile: { type: 'integer', required: true, default: '' },
email: { type: 'string', required: false, default: '' },
name: { type: 'string', required: true, default: '' }
}
};
参见 sails attributes对于您可以或不能放在模型描述中的内容
关于node.js - 类型对象的 sailsjs 模型属性,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41795602/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信