In my ongoing quest to wean myself of find_by_sql(), I now need to know
how to do multiple joins into a single table using the spiffy new
ActiveRecord query syntax.
As an example, imagine that an Edge is defined by its two vertices:
create_table “vertices”, :force => true do |t|
t.float “x”
t.float “y”
t.float “z”
end
create_table “edges”, :force => true do |t|
t.integer “vertex_a_id”
t.integer “vertex_b_id”
end
An SQL query to enumerate all points connected by edges might look like:
SELECT a.x as x0, a.y as y0, a.z as z0, b.x as x1, b.y as y1, b.z as
z1
FROM edges
JOIN vertices as a on a.id = edges.vertex_a_id
JOIN vertices as b on b.id = edges.vertex_b_id
Any idea how to express this query in ActiveRecord query syntax?
- ff
(As an aside, I find that AREL is pretty well documented, ActiveRecord
is not. And the relationship between the two, such as it is, is
entirely baffling. Maybe I’m just looking in the wrong places.)