Skip to content

Commit

Permalink
refetch if not all attributes filled
Browse files Browse the repository at this point in the history
  • Loading branch information
funny-falcon committed Sep 20, 2010
1 parent 6e3ce5b commit f984d25
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/identity_map/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ def if_id_map
end

private

def fetch_single(map, id)
if (obj = map[id]) && obj.attribute_names == column_names
obj
end
end

def fetch_from_map(map, ids)
result, not_cached = [], []
ids.each do |id|
if ( obj = map[id] )
if ( obj = fetch_single(map, id) )
result << obj
else
not_cached << id
Expand Down Expand Up @@ -74,7 +81,7 @@ def find_with_identity_map( *args )
end
end
else
map[ids]
fetch_single(map, ids)
end
end
end || find_without_identity_map(*args)
Expand Down
8 changes: 8 additions & 0 deletions spec/identity_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
d2 = Customer.find(:first, :conditions=>["id = ?", d1.id])
d1.__id__.should == d2.__id__
end

it "should refetch to fill missed attributes" do
d1 = Customer.find(:first, :select => 'id, name')
d1.read_attribute(:value).should be_nil
d2 = Customer.find(d1.id)
d2.__id__.should == d1.__id__
d1.value.should_not be_nil
end

context "creation and deletion:" do
before(:each) do
Expand Down

0 comments on commit f984d25

Please sign in to comment.