# Caching using ||=

**URL:** <https://www.ruby-forum.com/t/caching-using/119156>\
**Category:** Rails\
**Created:** [November 3, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/caching-using/119156 "2007-11-03T22:28:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 3, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/caching-using/119156/1 "2007-11-03T22:28:33Z")

</div>

Hi all

I’m a bit unsure about the caching with ||= …

I have the following code

class Member \< ActiveRecord::Base  
def allowed\_to?(right, model)

```
# Setup owned system rights if not already done
unless @owned_system_rights
  setup_owned_system_rights
else
  raise "asdf"
end
...

```

end

private  
def setup\_owned\_system\_rights  
@owned\_system\_rights = {}  
system\_groups.each do |system\_group|  
system\_group.system\_rights.each do |system\_right|  
@owned\_system\_rights[system\_right.model\_class.to\_sym] = {}  
unless @owned\_system\_rights[system\_right.model\_class.to\_sym]  
@owned\_system\_rights[system\_right.model\_class.to\_sym][system\_right.name.to\_sym]  
= true  
end  
end  
end  
end

I’d expect the raise “asdf” to be reached some time, but it’s not.  
What’s wrong here?

Thanks, Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 3, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/caching-using/119156/2 "2007-11-03T22:43:42Z")

</div>

On 3 Nov 2007, at 21:28, Joshua M. wrote:

> # Setup owned system rights if not already done
> 
> unless @owned\_system\_rights  
> setup\_owned\_system\_rights  
> else  
> raise “asdf”  
> end  
> …  
> end

This cache is instance specific - if you’re not calling allowed\_to  
more than once per instance of member per action then I woudn’t expect  
that line to be reached.

Fred

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 4, 2007, 1:17am UTC](https://www.ruby-forum.com/t/caching-using/119156/3 "2007-11-04T01:17:41Z")

</div>

Oh, I found the error, thanks. 🙂
