Path: | doc/release_notes/3.23.0.txt |
Last Update: | Mon Apr 29 02:10:38 +0000 2013 |
Artist.eager(:albums)
And you only want to eagerly load albums where the id is greater than or equal to some number provided by the user, you do:
min = params[:min].to_i Artist.eager(:albums=>proc{|ds| ds.where{id > min}})
This also works when eager loading via eager_graph:
Artist.eager_graph(:albums=>proc{|ds| ds.where{id > min}})
For eager_graph, the dataset is the dataset to graph into the current dataset, and filtering it will result in an SQL query that joins to a subquery.
You can also use dynamic customization while cascading to also eagerly load dependent associations, by making the hash value a single entry hash with a proc key and the value being the dependent associations to eagerly load. For example, if you want to eagerly load tracks for those albums:
Artist.eager(:albums=>{proc{|ds| ds.where{id > min}}=>:tracks})
albums = artist.albums_dataset.filter{id > min}
However, then there was no handling of caching, callbacks, or reciprocals. For example:
albums.each{|album| album.artist}
Would issue one query per album to get the artist, because the reciprocal association was not set. Now you can provide a block to the association method:
albums = artist.albums{|ds| ds.filter{id > min}}
This block is called with the dataset used to retrieve the associated objects, and should return a modified version of that dataset.
Note that ruby 1.8.6 doesn‘t allow blocks to take block arguments, so you have to pass the block as a separate proc argument to the association method if you are still using 1.8.6.
artist = Artist[1] Album.filter(:artist=>artist)
Since the above can also be accomplished with:
artist.albums
this may not seem like a big improvement, but it allows you to filter on multiple associations simultaneously:
Album.filter(:artist=>artist, :publisher=>publisher)
For simple many_to_one associations, the above is just a simpler way to do:
Album.filter(:artist_id=>artist.id, :publisher_id=>publisher.id)
Sequel supports this for all association types, including many_to_many and many_through_many, where a subquery is used, and it also works when composite key associations are used:
Album.filter(:artist=>artist, :tags=>tag)
This will give you the albums for that artist that are also tagged with that tag. To provide multiple values for the same association, mostly useful for many_to_many associations, you can either use separate filter calls or specify the conditions as an array:
Album.filter(:tags=>tag1).filter(:tags=>tag2) Album.filter([[:tags, tag1], [:tags, tag2]])
Sequel::Dataset.introspect_all_columns
model.hash_column = model.hash_column.merge(:foo=>:bar)
but not if you just modified the object directly:
model.hash_columns[:foo] = :bar
With this plugin, such modifications can be detected, at a potentially significant performance cost.
Sequel::Model::Associations::DatasetMethods. send(:remove_method, :complex_expression_sql)