# Can you use a mixin to add action methods to an ActionContro

**URL:** <https://www.ruby-forum.com/t/can-you-use-a-mixin-to-add-action-methods-to-an-actioncontro/104230>\
**Category:** Rails\
**Created:** [May 15, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/can-you-use-a-mixin-to-add-action-methods-to-an-actioncontro/104230 "2007-05-15T18:26:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 15, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/can-you-use-a-mixin-to-add-action-methods-to-an-actioncontro/104230/1 "2007-05-15T18:26:03Z")

</div>

I am trying to use a module to define a common interface between  
several ActiveRecord model classes, but I am having trouble setting  
the instance variable in the mixin functions. For example, I have a  
mixin,

module Photogenic  
def self.included( other\_mod )  
other\_mod.module\_eval do  
has\_many :images, :as =\> :photogenic, :dependent =\> :destroy do  
def showpictures  
@user = User.find(params[:id])  
@images = @user.images  
end  
end  
end  
end  
end

If I put the function show pictures in the action controller, it works  
fine, but if I move it to the module, the instance variable @user and  
@images is nil when I try to call it in the .rhtml file. Does anyone  
know why this is? Did I just implement the code incorrectly or am I  
using mixins incorrectly. Do I have access to params[:id] in the  
mixin?

I briefly read that you can only use mixins to define non-action  
methods. Does anyone know if this is true and why that would be the  
case?

Anyway, I would greatly appreciate any advice. I found examples of  
people using mixins to add methods to Active Record classes, but none  
of these functions tried to set instance variables or acted as action  
methods, so I feel I am incorrectly using the mixin.

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 15, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/can-you-use-a-mixin-to-add-action-methods-to-an-actioncontro/104230/2 "2007-05-15T18:32:02Z")

</div>

I don’t know enough Ruby to tell you about mixins, but here are two  
Rails-y thoughts:

1. You can put methods that you want in all controllers in  
controllers/application.rb.

2. If you want those methods to be available to your views, use  
helper\_method :showpictures in the same file.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 15, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/can-you-use-a-mixin-to-add-action-methods-to-an-actioncontro/104230/3 "2007-05-15T23:09:53Z")

</div>

> The reason I do not want to put it in the application.rb is that only  
> a few of my classes would have pictures, so I only want to give  
> photogenic functions to those specific classes. I am basically trying  
> to create a inheritance structure where three of my classes[ User,  
> Court, and Character ] have images and so have the photogenic mixin.

Why not just create an inheritance structure? RoR does support “single-  
table-inheritance.”

Just a thought…

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 16, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/can-you-use-a-mixin-to-add-action-methods-to-an-actioncontro/104230/4 "2007-05-16T20:17:04Z")

</div>

On 5/15/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From the results I have seen so far, it appears mixin functions cannot  
> change instance variables from the class they are includes them.

methods from a mixin can change instance variables. What is leading  
you to believe that they can’t?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 15, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/can-you-use-a-mixin-to-add-action-methods-to-an-actioncontro/104230/5 "2007-05-15T20:24:36Z")

</div>

Thanks for the advice.

I did consider both options you provided, but because of my situation,  
it seems like a mixin or something similar to it would work best for  
my case…though a mixin may not work at all. I realize now that I  
did not really describe my situation well. Hopefully I can clarify  
what I am trying to do without making the post too long.

The reason I do not want to put it in the application.rb is that only  
a few of my classes would have pictures, so I only want to give  
photogenic functions to those specific classes. I am basically trying  
to create a inheritance structure where three of my classes[ User,  
Court, and Character ] have images and so have the photogenic mixin.  
My situation is actually very similar to the case described in the  
website below, except I want to put action methods in the mixin.  
excerpt from:  
[http://johnwilger.com/2006/10/defining-interfaces-for-activerecord.html](http://johnwilger.com/2006/10/defining-interfaces-for-activerecord.html)  
#mixin  
module Commentable  
has\_many :comments, :as =\> :commentable do  
def approved find( :all, :conditions =\> [‘approved  
= ?’, true ] ) end  
end  
end  
#classes the include mixin  
class BlogEntry \< ActiveRecord::Base include Commentable  
end  
class ResearchArticle \< ActiveRecord::Base include Commentable  
end  
class Photograph \< ActiveRecord::Base include Commentable  
end  
#class I want to associate to the above classes  
class Comment \< ActiveRecord::Base  
belongs\_to :commentable, :polymorphic =\> true  
end

I also looked into putting these functions in a helper. In my case I  
also want to add methods that edit, add, and remove images, which may  
not be a good idea to put in the helper functions which are meant more  
for creating html. I was thinking about it, and if I can’t use  
mixins, I will most likely take your advice and put the functions more  
related to the views in the helper functions.

When I look into the details of using the mixin to achieve a pseudo  
inheritance where objects have associations, I can see many problems  
that can arise in addition to not being able to set the instance  
variables. I am guessing I will not be able to use a mixin for this  
case though it would really clean up my code if I could. Even though  
I may not be able to use a mixin, I am trying to figure out how mixins  
can be used for other siutations.

> From the results I have seen so far, it appears mixin functions cannot  
> change instance variables from the class they are includes them. If  
> that is true, does that mean mixins can really only be used to  
> calculate and return a value or object.

Anyway, thanks again for the help. Any information on how mixins can  
be used properly or any suggestions on alternative solutions to my  
problems will really help me solve this problem and help me learn more  
about Rails. This is actually the first time I’ve tried dealing with  
the modules and mixins, so I am trying to learn as much as possible  
and expect this first time to be slow.

Chris
