Can you use a mixin to add action methods to an ActionController?

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

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
#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 &lt; ActiveRecord::Base
  belongs_to :commentable, :polymorphic =&gt; 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

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.

--
Posted viahttp://www.ruby-forum.com/.

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....

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