model - Rails: Set a private value so it only calls the database once -



model - Rails: Set a private value so it only calls the database once -

i have next method check if user has admin access.

def has_admin_access?(user) user.present? && gym_users.where(:role_id => [3, 4]).where(['user_id = ? , date_ended null', user.id]).any? end

the problem comes when want phone call multiple times on page. how can set private value , create database phone call first time?

you can store result in hash, , if same user 1 time again homecoming result hash. this:

def has_admin_access?(user) @admin_hash ||= {} if (!@admin_hash.include?(user)) @admin_hash[user] = user.present? && gym_users.where(:role_id => [3, 4]).where(['user_id = ? , date_ended null', user.id]).any? end @admin_hash[user] end

ruby-on-rails-3 model accessor

Comments

Popular posts from this blog

java - How to pass parameters between Request-Scoped Controllers? -

actionscript 3 - Flex: moving a point with rotation doesnt change the point XY? -

ms access - C# - Using OleDbParameter on table name -