• 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。加了之后不报错了,新加的长度验证的测试也能通过了。

  • 噢 谢谢 我去看看

  • 😂 没字幕对我也太难了