Permalink
Browse files

Add the :eager_loader_key option to associations, useful for custom e…

…ager loaders

Advanced custom eager loaders may want to use a key for the eager
loader that isn't the same as the common :key option.  This can
be especially important in that if the eager loader key is empty,
there is no attempt made to eager load the association (since there
would be no matching records returned).
  • Loading branch information...
1 parent 57e2bf2 commit 8cf13f38247faa0db12d9d970d4a0b4b88a576ce @jeremyevans committed Mar 19, 2010
Showing with 11 additions and 3 deletions.
  1. +2 −0 CHANGELOG
  2. +9 −3 lib/sequel/model/associations.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Add the :eager_loader_key option to associations, useful for custom eager loaders (jeremyevans)
+
* Dataset#group_and_count no longer orders by the count (jeremyevans)
* Fix Dataset#limit on MSSQL 2000 (jeremyevans)
@@ -189,7 +189,7 @@ def eager_graph_lazy_dataset?
# The key to use for the key hash when eager loading
def eager_loader_key
- self[:key]
+ self[:eager_loader_key] ||= self[:key]
end
# The column(s) in the associated table that the key in the current table references (either a symbol or an array).
@@ -248,12 +248,16 @@ def can_have_associated_objects?(obj)
def default_key
:"#{underscore(demodulize(self[:model].name))}_id"
end
+
+ # The key to use for the key hash when eager loading
+ def eager_loader_key
+ self[:eager_loader_key] ||= primary_key
+ end
# The column in the current table that the key in the associated table references.
def primary_key
self[:primary_key] ||= self[:model].primary_key
end
- alias eager_loader_key primary_key
# One to many associations set the reciprocal to self when loading associated records.
def set_reciprocal_to_self?
@@ -337,7 +341,7 @@ def default_right_key
# The key to use for the key hash when eager loading
def eager_loader_key
- self[:left_primary_key]
+ self[:eager_loader_key] ||= self[:left_primary_key]
end
# many_to_many associations need to select a key in an associated table to eagerly load
@@ -518,6 +522,8 @@ def all_association_reflections
# and a hash of dependent associations. The associated records should
# be queried from the database and the associations cache for each
# record should be populated for this to work correctly.
+ # - :eager_loader_key - A symbol for the key column to use to populate the key hash
+ # for the eager loader.
# - :extend - A module or array of modules to extend the dataset with.
# - :graph_block - The block to pass to join_table when eagerly loading
# the association via eager_graph.

0 comments on commit 8cf13f3

Please sign in to comment.