Guest User

Untitled

a guest
Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. def current_account
  2. if session[:name].nil? or @current_account.nil?
  3. accounts = Account.find(:all,
  4. :conditions => ['name = ?',
  5. session[:name]])
  6. # Tricky here because detect will return nil, and the || operator will give me accounts.first
  7. # if no account exists for current_location
  8. @current_account = accounts.detect{ |each| each.location == current_location } || accounts.first
  9. else
  10. # The instance variable here, prevents me from needing to make that find call more then once per page
  11. # even if the current_account method is called many times in a view,
  12. @current_account
  13. end
  14. end
  15. # expose the method for the views
  16. helper_method :current_account
Add Comment
Please, Sign In to add comment