Caching using ||=

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

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

Oh, I found the error, thanks. :slight_smile: