Path: | doc/release_notes/4.8.0.txt |
Last Update: | Fri Oct 06 03:25:22 +0000 2017 |
Similarly, the many_through_many plugin now also offers a one_through_many association.
Unlike eager_graph (which uses LEFT OUTER JOINs by default), association_join uses INNER JOINs, but there are also association_*_join methods (e.g. association_left_join) for using different join types.
Similar to eager_graph, you can use cascading of associations or multiple associations.
Album.association_join(:artist, :tracks) Artist.association_left_join(:albums=>:tracks)
The :limit_strategy option works similarly to the :eager_limit_strategy option when eagerly loading. If set to true and the database supports window functions, it will join the current dataset to a subquery that uses a window function to correctly restrict the join to only those objects that fall within the association‘s limit/offset.
The :limit_strategy option is not on by default. It is possible for it to perform significantly worse than the default strategy (which uses array slicing in ruby). The :limit_strategy significantly changes the SQL used, and can change the results of the query if any filters/orders related to the association are used.
It‘s recommended you only use the :limit_strategy option if you are experiencing a bottleneck and you have benchmarked that it is faster and still produces the desired results.
Artist.eager_graph_with_options(:first_10_albums, :limit_strategy=>true) # SELECT artists.id, artists.name, # first_10_albums.id AS first_10_albums_id, # first_10_albums.name AS first_10_albums_name, # first_10_albums.artist_id, # first_10_albums.release_date # FROM artists # LEFT OUTER JOIN ( # SELECT id, name, artist_id, release_date # FROM ( # SELECT *, row_number() OVER (PARTITION BY tracks.album_id) # AS x_sequel_row_number_x # FROM albums # ) AS t1 WHERE (x_sequel_row_number_x <= 10) # ) AS first_10_albums ON (first_10_albums.artist_id = artists.id)
Artist.where(:first_10_albums=>Album[1]).all # SELECT * # FROM artists # WHERE (artists.id IN ( # SELECT albums.artist_id # FROM albums # WHERE ((albums.artist_id IS NOT NULL) AND (albums.id IN ( # SELECT id FROM ( # SELECT albums.id, row_number() OVER # (PARTITION BY albums.artist_id ORDER BY release_date) # AS x_sequel_row_number_x # FROM albums # ) AS t1 # WHERE (x_sequel_row_number_x <= 10) # )) AND (albums.id = 1))))
Artist.first_10_albums # SELECT * # FROM albums # WHERE ((albums.artist_id IN ( # SELECT artists.id FROM artists) # ) AND (albums.id IN ( # SELECT id FROM ( # SELECT albums.id, row_number() OVER # (PARTITION BY albums.artist_id ORDER BY release_date) # AS x_sequel_row_number_x # FROM albums # ) AS t1 # WHERE (x_sequel_row_number_x <= 10) # ))) # ORDER BY release_date