{"id":828,"date":"2022-08-30T15:07:51","date_gmt":"2022-08-30T15:07:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/ruby-on-rails-cancan-with-devise-error-undefined-method-find_by_name-for-role0x304ad18-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:51","modified_gmt":"2022-08-30T15:07:51","slug":"ruby-on-rails-cancan-with-devise-error-undefined-method-find_by_name-for-role0x304ad18-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/ruby-on-rails-cancan-with-devise-error-undefined-method-find_by_name-for-role0x304ad18-collection-of-common-programming-errors\/","title":{"rendered":"Ruby On Rails: CanCan with Devise error &#39;undefined method `find_by_name&#39; for #&lt;Role:0x304ad18&gt;-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m very confused of cancan, I tried to get roles for cancan out of my mysql database. I&#8217;m using a controller which got no actions like show\/edit\/destroy (it&#8217;s called a Non RESTful Controller I think?). I got a Role_id column in my user table and id and rolename in my roles table.<\/p>\n<p>All I get is this error:<\/p>\n<p>undefined method `find_by_name&#8217; for #<\/p>\n<pre><code>Ability.rb:\n class Ability\n      include CanCan::Ability\n      def initialize(user)\n        can :manage, :all if user.role? :admin\n    end\n    end\n<\/code><\/pre>\n<p>And here is my User.rb:<\/p>\n<pre><code>User.rb:\nclass User &lt; ActiveRecord::Base\n  # Include default devise modules. Others available are:\n  # :token_authenticatable,\n  # :lockable, :timeoutable and :omniauthable\n  devise :database_authenticatable, :registerable, :confirmable,\n         :recoverable, :rememberable, :trackable, :validatable\n\n  # Setup accessible (or protected) attributes for your model\n  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :firstname, :lastname, :birthday\n  # attr_accessible :title, :body\n\n  validates_uniqueness_of :username\n  belongs_to :role\n\ndef role?(role)\n    return !!self.role.find_by_name(role.to_s.camelize)\nend\nend\n<\/code><\/pre>\n<p>Role.rb:<\/p>\n<pre><code>class Role &lt; ActiveRecord::Base\n  has_many :users\n  attr_accessible :users, :name \nend\n<\/code><\/pre>\n<p>And here the controller I&#8217;m working in:<\/p>\n<pre><code>class WsdlController &lt; ApplicationController\n authorize_resource :class =&gt; false\n    $liga_id = 456\n    $liga_short = \"bl1\"\n    $saison = 2012\n\n    def connect\n        @client = Savon::Client.new(\"http:\/\/www.openligadb.de\/Webservices\/Sportsdata.asmx?WSDL\")\n        @output = \"\"\n    end\n\n    def get_all_for_new_saison\n        if @client.nil?\n            connect\n        end\n        #get_teams_by_league_saison\n        #get_matchdata_by_league_saison\n    end \nend\n<\/code><\/pre>\n<ol>\n<li>\n<p><code>find_by_name<\/code> is an <code>ActiveRecord<\/code> <strong>class method<\/strong>. (See the Active Record Query Interface Guide) You are invoking it like an <strong>instance method<\/strong>.<\/p>\n<p>Try changing this:<\/p>\n<pre><code>def role?(role)\n    return !!self.role.find_by_name(role.to_s.camelize)\nend\n<\/code><\/pre>\n<p>to something like this:<\/p>\n<pre><code>def role?(role_name)\n    return self.role.present? &amp;&amp; self.role.name == role_name.to_s\nend\n<\/code><\/pre>\n<p>Of course, in ruby, both <code>return<\/code> and <code>self<\/code> are optional.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:47:57. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m very confused of cancan, I tried to get roles for cancan out of my mysql database. I&#8217;m using a controller which got no actions like show\/edit\/destroy (it&#8217;s called a Non RESTful Controller I think?). I got a Role_id column in my user table and id and rolename in my roles table. All I get [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-828","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=828"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/828\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}