我将尝试简化问题,而不是将整个项目纳入范围,因此如果您有任何疑问,我会尝试更新更多信息。
我有 3 个正在使用的结构:
type Ticket struct{
ID bson.ObjectID `json:"id" bson:"_id"`
InteractionIDs []bson.ObjectId `json:"interactionIds" bson:"interactionIds"`
TicketNumber int `json:"ticketNumber" bson:"ticketNumber"`
Active bool `json:"active" bson:"active"`
// Other fields not included
}
type TicketInteraction struct{
ID bson.ObjectID `json:"id" bson:"_id"`
Active bool `json:"active" bson:"active"`
// Other fields not included
}
type TicketLookupTicket struct{
Ticket
Interactions []TicketInteraction `json:"interactions" bson:"interactions"`
}
然后我有一个 mgo 管道,我正在使用它来将两个集合“连接”在一起
var tickets []TicketLookupTicket
c := mongodb.NewCollectionSession("tickets")
defer c.Close()
pipe := c.Session.Pipe(
[]bson.M{
"$lookup": bson.M{
"from": "ticketInteractions",
"localField": "interactionIds",
"foreignField": "_id",
"as": "interactions",
}
},
)
pipe.All(&tickets)
现在应该使用数据库的结果填充工单,但实际发生的只是每张工单中的交互已被填充。它最终看起来像这样:
[
{
ID: ObjectIdHex(""),
InteractionIDs: nil,
TicketNumber: 0,
Active: false,
// Other Fields, all with default values
Interactions: []{
{
ID: ObjectIdHex("5a441ffea1c9800148669cc7"),
Active: true,
// Other Fields, with appropriate values
}
}
}
]
现在,如果我在 TicketLookupTicket 结构中手动声明一些 Ticket 结构字段,这些字段将开始填充。例如:
type TicketLookupTicket struct{
Ticket
ID bson.ObjectId `json:"id" bson:"_id"`
TicketNumber int `json:"ticketNumber" bson:"ticketNumber"`
Active bool `json:"active" bson:"active"`
Interactions []TicketInteraction `json:"interactions" bson:"interactions"`
}
现在 ID、TicketNumber 和 Active 将开始填充,但其余字段不会。
谁能解释为什么导入的 Ticket 字段与声明字段的行为不同?
最佳答案
Per the documentation ,需要在字段中添加inline标签:
type TicketLookupTicket struct{
Ticket `bson:",inline"`
Interactions []TicketInteraction `json:"interactions" bson:"interactions"`
}
inlineInline the field, which must be a struct or a map. Inlined structs are handled as if its fields were part of the outer struct. An inlined map causes keys that do not match any other struct field to be inserted in the map rather than being discarded as usual.
默认情况下,它假定嵌入字段 Ticket 应由 TicketLookupTicket.Ticket 中的对象填充。
关于Golang 导入的字段与标准字段声明的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48287373/
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?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,如果没有检查,请帮助我,非常感谢,谢谢
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])
我正在尝试找到一种方法来规范化字符串以将其作为文件名传递。到目前为止我有这个:my_string.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.gsub(/[^a-z]/,'_')但第一个问题:-字符。我猜这个方法还有更多问题。我不控制名称,名称字符串可以有重音符、空格和特殊字符。我想删除所有这些,用相应的字母('é'=>'e')替换重音符号,并将其余的替换为'_'字符。名字是这样的:“Prélèvements-常规”“健康证”...我希望它们像一个没有空格/特殊字符的文件名:“prelevements_routin
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.
两个gsub产生不同的结果。谁能解释一下为什么?代码也可在https://gist.github.com/franklsf95/6c0f8938f28706b5644d获得.ver=9999str="\tCFBundleDevelopmentRegion\n\ten\n\tCFBundleVersion\n\t0.1.190\n\tAppID\n\t000000000000000"putsstr.gsub/(CFBundleVersion\n\t.*\.).*()/,"#{$1}#{ver}#{$2}"puts'--------'putsstr.gsub/(CFBundleVersio