# Mixin behavior

**URL:** <https://www.ruby-forum.com/t/mixin-behavior/91891>\
**Category:** Rails\
**Created:** [April 13, 2007, 4:43am UTC](https://www.ruby-forum.com/t/mixin-behavior/91891 "2007-04-13T04:43:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [April 13, 2007, 4:43am UTC](https://www.ruby-forum.com/t/mixin-behavior/91891/1 "2007-04-13T04:43:40Z")

</div>

Hi, all,

I need to DRY up some code in a few models. My first inclination,  
having come from the Java world, was to go with an abstract class.  
After a bit of reading, I then decided to follow the mixin route  
instead. Now I’m running into trouble, most likely from a few basic  
misunderstandings.

There are three models I will be using to average and count rating  
data by executing a scheduled job. Basically, I want to get averages  
and counts for the last week, the last month, and all time, and dump  
the data into isolated tables to allow for quick reads. The logic is  
pretty much identical for each instance. I just have a variance of  
dates and a different table for each set of summary data. So I  
defined a module (messy, I know):

* * *

module RatingTotals::StoryRatingTotals

def refresh\_story\_totals(date = Date.new(2007, 1, 1))  
# Removed most the code.  
ratings = Rating.find(:all,  
:select =\> 'rateable\_id, AVG(rating) as  
average\_rating, COUNT(id) as count ',  
:conditions =\> ["rateable\_type = ‘story’ AND  
created\_at \>= ? ", date],  
:group =\> 'rateable\_id ')

```
totals = []
ratings.each do |rating|
  totals << self.new(:story_id => rating.rateable_id,
                     :rating => rating.average_rating,
                     :count => rating.count)
end

self.transaction do
  self.delete_all
  totals.each do |total|
    total.save
  end
end

```

## end end

Then my models are pretty simple. I’d like to get a better aliasing  
technique in place, but this at least executes:

* * *

class AllTimeStoryRatingTotal \< ActiveRecord::Base  
belongs\_to :story

extend StoryRatingTotals

## def self.refresh\_totals self.refresh\_story\_totals end end

class WeeklyStoryRatingTotal \< ActiveRecord::Base  
belongs\_to :story

extend StoryRatingTotals

## def self.refresh\_totals self.refresh\_story\_totals(1.week.ago) end end

class MonthlyStoryRatingTotal \< ActiveRecord::Base  
belongs\_to :story

extend StoryRatingTotals

## def self.refresh\_totals self.refresh\_story\_totals(1.month.ago) end end

When testing through the console, I can only execute a single one of  
the class methods ( e.g. MonthlyStoryRatingTotal.refresh\_totals).  
Whichever class method I invoke first can be executed ad naseum, but  
if I try to execute one of the other class refresh\_totals methods they  
are blocked out with the most classic of error messages:

* * *

## ArgumentError: RatingTotals is not missing constant StoryRatingTotals!

What I suspect is that I’m confused on my ability to use ‘self’ within  
modules. Another thought is that the way scoping works under the  
console may not be true to real life. Though I also attempted to pass  
in the class type as a parameter and I received the same errors.

Any thoughts on the most elegant way to abstract these class methods  
successfully?

Thanks,
