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

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -