noobiehacker
noobiehacker

Reputation: 1109

Using Ruby Driver to create a complicated query in Rethinkdb's Reql

I am using the ruby driver for rethink db and I am running into some problem with nested elements that has arrays.

I have my data structured like this

[{"family_name"=>"Adam"},
 {"family_name"=>"Bobby"},
 {"family_name"=>"Crissy",
  "groups"=>
   [{"logo_url"=>"https://www.logourl.com/1111.png",
     "name"=>"Sample Name 1",
     "profile_url"=>"https://www.profileurl.com/groups?gid=1111"},
    {"logo_url"=>"https://www.logourl.com/2222.png",
     "name"=>"Sample Name 2",
     "profile_url"=>"https://www.profileurl.com/groups?gid=2222"},
    {"logo_url"=>"https://www.logourl.com/3333.png",
     "name"=>"Sample Name 3",
     "profile_url"=>"https://www.profileurl.com/groups?gid=3333"},
   ]},
 {"family_name"=>"Bobby"},
 {"family_name"=>"Hillton",
  "groups"=>
   [{"logo_url"=>"https://www.logourl.com/4444.png",
     "name"=>"Sample Name 1",
     "profile_url"=>"https://www.profileurl.com/groups?gid=4444"},
    {"logo_url"=>"https://www.logourl.com/5555.png",
     "name"=>"Sample Name 2",
     "profile_url"=>"https://www.profileurl.com/groups?gid=55555"},
    {"logo_url"=>"https://www.logourl.com/6666.png",
     "name"=>"Sample Name 3",
     "profile_url"=>"https://www.profileurl.com/groups?gid=12345"},
    ]}]

The desired result is to return the row that has 'profile_url' link that ends with "12345". Using a mix of Ruby and Javascript, I have attempted this :

'r.table("profiles").filter{ |row| row['groups'].contains(r.js('function(group) { return group('profile_url').match("12345$") }'))}.run(conn)'

However, it still is not returning the result, is there a clear way to use it with the ruby driver???

The desired result would be

[{"family_name"=>"Hillton",
  "groups"=>
   [{"logo_url"=>"https://www.logourl.com/4444.png",
     "name"=>"Sample Name 1",
     "profile_url"=>"https://www.profileurl.com/groups?gid=4444"},
    {"logo_url"=>"https://www.logourl.com/5555.png",
     "name"=>"Sample Name 2",
     "profile_url"=>"https://www.profileurl.com/groups?gid=55555"},
    {"logo_url"=>"https://www.logourl.com/6666.png",
     "name"=>"Sample Name 3",
     "profile_url"=>"https://www.profileurl.com/groups?gid=12345"},
    ]}]

Please help and thanks in advance!!! :D

Upvotes: 1

Views: 37

Answers (1)

mlucy
mlucy

Reputation: 5289

I think this will do what you want:

r.table('profiles').filter {|row|
  row['groups'].contains {|group|
    group['profile_url'].match('12345$')
  }
}

Upvotes: 3

Related Questions