这错误信息就不全吧,这 SQL 后面应该还有些信息的啊
但是判断完存在后一般不是会按结果做不同处理吗?用 present?会拖慢不存在时的处理吧,exists?看起来速度挺快的,相对不会拖慢太多存在时的处理
User.eager_load(:group).where(group: {type: 'XXX'})
试试这样?
对结果用 each 不就可以分出来了吗
require 'json'
json = '[ { "kind": "Service", "apiVersion": "v1", "metadata": { "name": "redis-slave", "namespace": "development3", "selfLink": "/api/v1/namespaces/default/services/redis-slave", "uid": "bdb80a8f-db93-11e4-b293-f8b156af4ae1",
"creationTimestamp": "2015-04-05T13:00:31Z", "labels": { "name": "redis", "role": "slave" } }, "spec": { "ports": [ { "name": "", "protocol": "TCP", "port": 6379, "targetPort": "redis-server" } ], "selector": { "name": "redis", "role": "slave" }, "clusterIP": "10.97.0.141", "sessionAffinity": "None" }, "status": {} },{ "kind": "Endpoints", "apiVersion": "v1", "metadata": { "name": "myendpoint", "namespace": "default", "selfLink": "/api/v1/namespaces/default/endpoints/myendpoint", "uid": "59d05b48-dadb-11e5-937e-18037327aaeb", "creationTimestamp": "2016-02-24T09:45:34Z" }, "subsets": [ { "addresses": [ { "ip": "172.17.0.25" } ], "ports": [ { "name": "https", "port": 6443, "protocol": "TCP" } ] } ] } ] '
JSON.parse(json).group_by {|h| h["kind"]}
# 结果
{"Service"=>[{"kind"=>"Service", "apiVersion"=>"v1", "metadata"=>{"name"=>"redis-slave", "namespace"=>"development3", "selfLink"=>"/api/v1/namespaces/default/services/redis-slave", "uid"=>"bdb80a8f-db93-11e4-b293-f8b156af4ae1", "creationTimestamp"=>"2015-04-05T13:00:31Z", "labels"=>{"name"=>"redis", "role"=>"slave"}}, "spec"=>{"ports"=>[{"name"=>"", "protocol"=>"TCP", "port"=>6379, "targetPort"=>"redis-server"}], "selector"=>{"name"=>"redis", "role"=>"slave"}, "clusterIP"=>"10.97.0.141", "sessionAffinity"=>"None"}, "status"=>{}}], "Endpoints"=>[{"kind"=>"Endpoints", "apiVersion"=>"v1", "metadata"=>{"name"=>"myendpoint", "namespace"=>"default", "selfLink"=>"/api/v1/namespaces/default/endpoints/myendpoint", "uid"=>"59d05b48-dadb-11e5-937e-18037327aaeb", "creationTimestamp"=>"2016-02-24T09:45:34Z"}, "subsets"=>[{"addresses"=>[{"ip"=>"172.17.0.25"}], "ports"=>[{"name"=>"https", "port"=>6443, "protocol"=>"TCP"}]}]}]}
这个符合你的要求吗
嗯,主要是 railstutorial 里面也是这么写的 ,我就依样画葫芦了。在 Stack Overflow 上找到了解决方法:在 password 的 validates 后面加上 allow_nil: true。加了之后不报错了,新加的长度验证的测试也能通过了。
噢 谢谢 我去看看
没字幕对我也太难了