module Sequel::Plugins::LazyAttributes::InstanceMethods
Private Instance Methods
lazy_attribute_lookup(a, opts=OPTS)
click to toggle source
If the model was selected with other model objects, eagerly load the attribute for all of those objects. If not, query the database for the attribute for just the current object. Return the value of the attribute for the current object.
# File lib/sequel/plugins/lazy_attributes.rb 88 def lazy_attribute_lookup(a, opts=OPTS) 89 table = opts[:table] || model.table_name 90 selection = Sequel.qualify(table, a) 91 92 if base_ds = opts[:dataset] 93 ds = base_ds.where(qualified_pk_hash(table)) 94 else 95 base_ds = model.dataset 96 ds = this 97 end 98 99 if frozen? 100 return ds.get(selection) 101 end 102 103 if retrieved_with 104 primary_key = model.primary_key 105 composite_pk = true if primary_key.is_a?(Array) 106 id_map = {} 107 retrieved_with.each{|o| id_map[o.pk] = o unless o.values.has_key?(a) || o.frozen?} 108 predicate_key = composite_pk ? primary_key.map{|k| Sequel.qualify(table, k)} : Sequel.qualify(table, primary_key) 109 base_ds. 110 select(*(Array(primary_key).map{|k| Sequel.qualify(table, k)} + [selection])). 111 where(predicate_key=>id_map.keys). 112 naked. 113 each do |row| 114 obj = id_map[composite_pk ? row.values_at(*primary_key) : row[primary_key]] 115 if obj && !obj.values.has_key?(a) 116 obj.values[a] = row[a] 117 end 118 end 119 end 120 values[a] = ds.get(selection) unless values.has_key?(a) 121 values[a] 122 end