0

Sample on http://www.rethinkdb.com/docs/table-joins/, titled "Using subqueries" does not work as expected. Apart from the typo on the word lambda can you suggest the fix ?

ZeroGraviti
  • 1,047
  • 2
  • 12
  • 28
  • This is the response I get on my irb ruby console: `irb(main):050:0> r.table("companies").get(id).merge(lamdba company: irb(main):051:1* { 'employees': r.table('employees').get_all(company['id'], irb(main):052:3* index='company_id').coerce_to('array') } irb(main):053:1> ).run() SyntaxError: (irb):51: syntax error, unexpected ':', expecting tASSOC { 'employees': r.table('employees').get_all(company['id'], ^ (irb):52: syntax error, unexpected '}', expecting $end from /usr/bin/irb:12:in
    ' irb(main):054:0> `
    – ZeroGraviti May 28 '15 at 05:09

1 Answers1

1

The example posted in http://www.rethinkdb.com/docs/table-joins/ is a Python example. If you want to try out the example in Ruby, try the entering the following query into irb:

r.table("companies").get(id).merge{ |company| { 
     :employees => r.table('employees')
       .get_all(company['id'], :index => 'company_id')
       .coerce_to('array') }
  }.run(Conn)

The result of that query should look something like this:

irb(main):254:0> r.table("companies").get(id).merge{ |company| {
irb(main):255:2*      :employees => r.table('employees')
irb(main):256:2>        .get_all(company['id'], :index => 'company_id')
irb(main):257:2>        .coerce_to('array') }
irb(main):258:1>   }.run(Conn)
=> {"company"=>"Starfleet", "company_id"=>"064058b6-cea9-4117-b92d-c911027a725a", "employees"=>[], "id"=>"064058b6-cea9-4117-b92d-c911027a725a", "name"=>"Jean-Luc Picard", "rank"=>"captain", "type"=>"paramilitary"}

Make sure that all the appropriate tables and indexes have been created before you run the query:

// Create the tables
r.table_create("companies").run(Conn)
r.table_create("employees").run(Conn)

// Create the index
r.table("employees").index_create("company_id").run(Conn)

// Insert a document
r.table("companies").insert({
    "id": "064058b6-cea9-4117-b92d-c911027a725a",
    "name": "Jean-Luc Picard",
    "company_id": "064058b6-cea9-4117-b92d-c911027a725a",
    "rank": "captain",
    "company": "Starfleet",
    "type": "paramilitary"
}).run(Conn)
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42
  • Works, thanks. The instructions on the page can include the creation of the index `company_id` which seems to be missing, however. – ZeroGraviti May 29 '15 at 05:00