# Access to mixed in method straight after its been mixedin?

**URL:** <https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859>\
**Category:** Rails\
**Created:** [September 7, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859 "2006-09-07T13:17:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 7, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859/1 "2006-09-07T13:17:46Z")

</div>

Hi,

Can’t I access methods directly after they have been mixed in?

In the code below I get an error at the marked location “undefined local  
variable or method `current\_user’ for ApplicationController:Class” even  
though I’ve already mixed in the variable into the class. Note that  
when I use “current\_user.login” in another place, for example in an  
action in a specific controller, it works fine, but just not here in the  
ApplicationController directly after the mix in.

Can you spot why at all? Tks in advance.

# ============================================ class ApplicationController \< ActionController::Base include ActiveRbacMixins::ApplicationControllerMixin u = current\_user.login \<=== ERROR OCCURS HERE end

# module ActiveRbacMixins module ApplicationControllerMixin def self.included(base) base.class\_eval do protected def current\_user return @active\_rbac\_user unless @active\_rbac\_user.nil? @active\_rbac\_user = if session[:rbac\_user\_id].nil? then ::AnonymousUser.instance else ::User.find(session[:rbac\_user\_id]) end return @active\_rbac\_user end end end end end

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 7, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859/2 "2006-09-07T15:16:54Z")

</div>

Greg H. wrote:

> ```
> protected
> end
> end
> 
> ```
> 
> # end end

current\_user is an ApplicationController instance method, which  
you can’t use in the class context of a class definition.  
Perhaps you want to put the setting of u (or @u) in a before\_filter.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 7, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859/3 "2006-09-07T21:07:52Z")

</div>

Tks - but I still don’t quite understand then why the following works  
say  
however:

# ===== include ActiveRbacMixins::ApplicationControllerMixin @a = 1 @b = @a \<=== WORKS u = current\_user.login \<=== DOESN’T WORK

I thought conceptually that the “include” along with the mixin’ed method  
of  
“base.class\_eval do” was effectively injecting “current\_user” into the  
class  
so that it would be effectively equivalent to the “@a” variable? Like  
the  
“@a=1” line seems to get run?

Is it really a basic “class” level context ruby rule that I’m not  
understanding correctly? or is it something to do with the mixin  
approach  
and how the “base.class\_eval do” concept works?

BTW - I thought I read that an “include” includes the methods as if they  
were a “superclass” - would it be something to do with this? (i.e. in  
terms  
of understanding why I get the error on the line mentioned)

Thanks again  
Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 8, 2006, 6:28am UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859/4 "2006-09-08T06:28:53Z")

</div>

Greg H wrote:

> class ContactsController \< ApplicationController  
> before\_filter { Suberb.current\_user\_gregs = current\_user.login }  
> \<== “ERROR HERE”

If you write a filter as block you have to use a controller parameter  
in order to access current controller instance methods.

before\_filter { |c| Suberb.current\_user\_gregs = c.current\_user.login  
}

> def edit  
> Suberb.current\_user\_gregs = current\_user.login \<==  
> ACTUALLY WORKS HERE ?  
> end

Yes, edit is a controller instance method that has access to  
current\_user, another controller instance method.

You can now write:

before\_filter :edit

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 8, 2006, 12:06am UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859/5 "2006-09-08T00:06:19Z")

</div>

PS. just did some more testing and have proved to myself that  
“current\_user”  
(created by a mixin with the “class\_eval” concept) can’t be accessed via  
a  
“before\_filter”, however and can access it and see it find if I put it  
within a method. See code snippet below.

Can anyone explain why this occurs? Also how can I get access to this  
variable in a “before\_filter” or directly within the superclass, as I  
dont’  
want to have to try to put the same line of code in all the different  
actions ☹

class ContactsController \< ApplicationController  
before\_filter { Suberb.current\_user\_gregs = current\_user.login }  
\<==  
“ERROR HERE”

def edit  
Suberb.current\_user\_gregs = current\_user.login \<==  
ACTUALLY WORKS HERE ?  
end  
.  
.  
.  
end

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 8, 2006, 7:06am UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859/6 "2006-09-08T07:06:59Z")

</div>

Greg H wrote:

> Tks - but I still don’t quite understand then why the following works  
> say however:
> 
> # ===== include ActiveRbacMixins::ApplicationControllerMixin @a = 1 @b = @a \<=== WORKS u = current\_user.login \<=== DOESN’T WORK

You are in a class not an instance context here, so you  
can’t access the current\_user instance method.

Instance variables are normally associated with an  
instance of a class object, being initialized in the  
class’ initialize method. But, as you have done, they can  
also be defined in the class context of a class definition.  
In this case the variables are like double-@ class variables,  
but are local to the class, and not accessible by descendent  
classes. e.g:

```
class A
       @a = 1
       @@a = 2
       def A.d
         p @a
         p @@a
       end
     end

     class B < A
     end

```

A.d prints

1  
2

While B.d prints

nil  
2

> I thought conceptually that the “include” along with the mixin’ed method  
> of " base.class\_eval do" was effectively injecting “current\_user” into  
> the class so that it would be effectively equivalent to the “@a”  
> variable? Like the “@a=1” line seems to get run?

class\_eval injects its receiver as self. Here the receiver is base,  
which  
is ApplicationController. Inside the class\_eval block the current\_user  
method has been defined as “def current\_user”, not “def  
self.current\_user”,  
so when the module is included current\_user becomes an  
ApplicationController  
instance method.

It may have been done for some other reason, but in the way you’re  
including the module there was no need to define self.included and  
use class\_eval. All that needed to be done was to def the current\_user  
method inside the mixin module.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 8, 2006, 11:15am UTC](https://www.ruby-forum.com/t/access-to-mixed-in-method-straight-after-its-been-mixedin/67859/7 "2006-09-08T11:15:43Z")

</div>

thanks Mark
