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.
has_many :comments, :as => :commentable do
def approved find( :all, :conditions => [‘approved
= ?’, true ] ) end
#classes the include mixin
class BlogEntry < ActiveRecord::Base include Commentable
class ResearchArticle < ActiveRecord::Base include Commentable
class Photograph < ActiveRecord::Base include Commentable
#class I want to associate to the above classes
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
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.