答案 0 :(得分:1)
http://www.rethinkdb.com/docs/table-joins/中发布的示例是一个Python示例。如果您想在Ruby中试用该示例,请尝试将以下查询输入irb:
r.table("companies").get(id).merge{ |company| {
:employees => r.table('employees')
.get_all(company['id'], :index => 'company_id')
.coerce_to('array') }
}.run(Conn)
该查询的结果应如下所示:
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"}
确保在运行查询之前已创建所有适当的表和索引:
// 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)