# RESTful ACL Question (get an error author not found)

**URL:** <https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406>\
**Category:** Rails\
**Created:** [July 7, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406 "2008-07-07T17:22:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 7, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/1 "2008-07-07T17:22:22Z")

</div>

Hi everybody,

hope somebody can help me.  
I always get this error, even every relation is set in the models:

NameError Exception: undefined local variable or method `author’

as soon as I do something like this, it works…  
(when I debug, I don’t have access to the actual instance attributes,  
either the relations)

## this works

def self.is\_readable\_by(user, object = nil)  
true if user.eql?(object.author) || user.admin  
end

## instead of (manual, wiki of RESTful\_ACL)

def self.is\_readable\_by(user, object = nil)  
true if user.eql?(author) || user.admin  
end

…is that a bug, or am I doing something wrong here?  
can it be done easier?

thanks

Rafael

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 7, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/2 "2008-07-07T18:04:36Z")

</div>

> end
> 
> …is that a bug, or am I doing something wrong here?  
> can it be done easier?

hi,

first of all, from your code i understand that you have a class called  
“object”. This should really be avoided since there already is a class  
called object (the ruby core class, that is), so you will actually  
reopen the ruby object-class.

to your question: you have to specifiy the object either with  
self.author or object.author. that’s the only way as far as i know…

i hope this helps! smn

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 7, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/3 "2008-07-07T18:52:42Z")

</div>

I don’t see an indication of there being a class named “Object.” Yes  
there is a local variable named “object.”

What I can’t see from the code in the OP is what kind of object is  
being passed into the method referred to by “object.” If this is an  
ActiveRecord object containing an association to an “Author” object  
then you would need object.author. user.eql?(author) would be  
attempting to refer to a local variable named “author” which doesn’t  
exist.

I’m also guessing that this method might be defined in an ActiveRecord  
subclass and you are expecting to be able to access the “author”  
method directly. If that’s the case then you should realize that  
“self.is\_readable\_by(…)” is a class method so you wouldn’t want to  
try to access an instance method such as the “author” association.  
This is why an object instance is being passed into the class method.

I am also making the assumption that the object passed into “object”  
has an association to an Author object, which appears to be the case  
since your solution works.

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 7, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/4 "2008-07-07T22:39:10Z")

</div>

On Jul 7, 10:21 am, Rafael [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> …is that a bug, or am I doing something wrong here?  
> can it be done easier?

The issue is that you’re defining your method as a class method, not  
an instance method; i.e, you should do

def is\_readable\_by(user)

not

def self.is\_readable\_by(user)

This is because author is an instance method, and so is not reachable  
from class methods.

I’m skimming the surface of class vs. instance methods so if you need  
more details, just let me know.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

[purpleworkshops.com](http://purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 8, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/5 "2008-07-08T14:06:41Z")

</div>

so it is really, like that, the object has to be called.  
so I take it like I figured out:  
object.author

thanks all for your help!

Rafael

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 8, 2008, 2:40am UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/6 "2008-07-08T02:40:46Z")

</div>

I have to admit that Ruby’s use of the “self” keyword to define class  
methods feels a bit awkward to me. However, that’s probably because I  
learned Objective C before diving into Ruby. Objective C uses the  
“self” keyword to mean “this instance” in a similar way as Java uses  
the “this” keyword. It was just a matter of getting used to it.

I personally like the syntax of the Objective C language where you  
would have something like this:

- myClassMethod  
{  
// implementation  
}

- myInstanceMethod  
{  
// implementation  
}

Methods with plus signs are class methods, where methods with minus  
signs are instance methods. This way to intent is explicit and clear.  
Class and instance methods are treated with the same “respect” in a  
way.

But, at the end of the day it’s all just all semantics.

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 8, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/7 "2008-07-08T22:12:01Z")

</div>

Jeff C. wrote:

> The issue is that you’re defining your method as a class method, not  
> an instance method; i.e, you should do
> 
> def is\_readable\_by(user)
> 
> not
> 
> def self.is\_readable\_by(user)

Just to clarify why RESTful\_ACL defines the is\_readable\_by method as a  
Class-level method…

This method is responsible for securing both object#index and  
object#show. Since object#index operates on a set of objects, it needs  
to be class level. On the other hand, since it also secures object#show  
(one object), it needs to have access to a potential object, hence  
(object = nil)

Hope this clarifies 😉

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 8, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/8 "2008-07-08T14:34:28Z")

</div>

> {  
> // implementation
> 
> }

The Ruby use of self is a bit more complicated. It’s closer to java  
that it appears that you understand. It refer to the current object  
in a similar way to java, it’s just that when you are in the context  
of a class definition, self happens to refer to the class that is  
being defined. You could just as easily declare it this way if it’s  
easier to understand:

class MyClass  
def MyClass.my\_class\_method  
…  
end

def self.my\_other\_class\_method  
…  
end  
end

The flexibility of self in Ruby permits some other useful things. For  
example, look at how ActiveRecord spins up getters and setters for db-  
backed attributes. It hooks method\_missing and then uses  
‘self’ (which in this context refers to the current instance) to as  
the receiver for setter/getter definitions.

I think the confusion arises because the ‘def self.bar’ use of self  
for declaring class methods is one of the most common uses.

---

<div class="post-metadata">

**Author:** ![rafael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@rafael](https://www.ruby-forum.com/u/rafael)\
**Post date:** [July 9, 2008, 2:26am UTC](https://www.ruby-forum.com/t/restful-acl-question-get-an-error-author-not-found/142406/9 "2008-07-09T02:26:18Z")

</div>

If you really want to grok this, read the chapter on ‘self’ in Ruby for  
Rails. The trippy things (for me) were realizing that 1) in addition to  
being blueprints for creating objects (instances), ruby classes are  
objects in their own right, and 2) class definitions are _executed_.

You can also play with this:

# =========================

puts(“In open code: self is now #{self}”)

def top\_level\_method  
puts(“In top\_level\_method: self is now #{self}”)  
end

top\_level\_method

class MyClass  
puts(“In MyClass: self is now #{self}”)

def MyClass.my\_class\_method  
puts(“In MyClass.my\_class\_method: self is now #{self}”)  
end

def my\_instance\_method  
puts(“In my\_instance\_method: self is now #{self}”)  
end  
end

MyClass.my\_class\_method

fred = MyClass.new

fred.my\_instance\_method

# =========================

I’m not sure what’s behind the idiom for using ‘self’ in class method  
definitions. I guess it makes it easier if you later want to rename the  
class. 😉
