Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##
- # excerpt from user.rb
- # this is how roles is defined
- has_and_belongs_to_many :roles
- # and this the has_role? function
- def has_role?(role)
- if role.is_a?(String)
- @@roles ||= {}
- @@roles[role] ||= Role.find_by_title(role)
- role = @@roles[role]
- end
- roles.include?(role) # this is the line of failure, but only when user.roles was not called before
- end
- ##
- # console output
- # when calling user.roles before calling has_role? it works fine
- >> u = User.find 1
- => #<User id: 1, login: "mk", ....>>>
- u.roles
- => [#<Role id: 2, title: "admin">, #<Role id: 5, title: "same-ip">, #<Role id: 4, title: "tester">, #<Role id: 3, title: "user">]
- >> u.has_role?('admin')
- => true
- # but it crashes when not
- >> User.find(1).has_role?('admin')
- ActiveRecord::StatementInvalid: TypeError: wrong argument type Hash (expected String): conditions`roles`.`id` = 2sqlSELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.id = `roles_users`.role_id WHERE (`roles`.`id` = 2) AND (`roles_users`.user_id = 1 ) LIMIT 1offsetselect`roles`.idlimit1
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:151:in `log'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:299:in `execute'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:499:in `select'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:55:in `select_all'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/base.rb:579:in `exists?'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:258:in `send'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:258:in `method_missing_without_paginate'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/base.rb:1806:in `with_scope'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:168:in `send'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:168:in `with_scope'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:254:in `method_missing_without_paginate'
- from /home/deploy/sauspiel-rails/vendor/plugins/will_paginate/lib/will_paginate/finder.rb:164:in `method_missing'
- from /home/deploy/sauspiel-rails/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:218:in `include?'
- from /home/deploy/sauspiel-rails/app/models/user.rb:575:in `has_role?'
- from (irb):15
Add Comment
Please, Sign In to add comment