# Sharing a method, model, module or?

**URL:** <https://www.ruby-forum.com/t/sharing-a-method-model-module-or/183440>\
**Category:** Rails\
**Created:** [February 14, 2010, 7:58am UTC](https://www.ruby-forum.com/t/sharing-a-method-model-module-or/183440 "2010-02-14T07:58:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 14, 2010, 7:58am UTC](https://www.ruby-forum.com/t/sharing-a-method-model-module-or/183440/1 "2010-02-14T07:58:39Z")

</div>

I need some overview thinking help.

I have Pictures model/controller but generally, I want to show pictures  
via a partial displayed in other controller views and I want to do  
things like choose the picture based upon various concepts of relevance.  
I also want to update a ‘view\_count’ column in the Pictures model each  
time it is displayed.

So if in a partial in /users controller, I would love to be able to call  
some Picture method (either model or controller but probably controller  
is the more logical choice) but I can’t figure a way to do this…

\<%= PictureController.some\_method %\> clearly gives me an error

if I try to put that code into a model…

\<%= Picture.some\_method %\> I am still up a creek without a paddle

this works but clearly doesn’t belong in a view…

\<% @pic\_1 = Picture.find(:first, :conditions =\> [“active =  
true”], :order =\> ‘view\_count’); @pic\_1.view\_count += 1; @pic\_1.save! %\>

And of course my problem is that I want to use this ‘partial’ in a bunch  
of different controllers & actions which is why I don’t want to include  
it in each controller/method.

What is the best way to handle something like this?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 14, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/sharing-a-method-model-module-or/183440/2 "2010-02-14T13:19:20Z")

</div>

On Feb 14, 6:56 am, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= Picture.some\_method %\> I am still up a creek without a paddle
> 
> this works but clearly doesn’t belong in a view…
> 
> \<% @pic\_1 = Picture.find(:first, :conditions =\> [“active =  
> true”], :order =\> ‘view\_count’); @pic\_1.view\_count += 1; @pic\_1.save! %\>

Why can’t you stick all that in a class method on Picture ?

Fred

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 14, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/sharing-a-method-model-module-or/183440/3 "2010-02-14T17:08:12Z")

</div>

It depends on what you want the method to do.  
Inside the Picture class, this should work:

class Picture \< ActiveRecord::Base

# …

def self.get\_most\_viewed\_picture  
pic = Picture.find(:first, :conditions =\> [“active = ?”, true],  
:order  
=\> ‘view\_count DESC’)  
pic.increment! :view\_count  
pic  
end

# …

end

In the controller:

class UsersController \< ApplicationController  
def index  
@picture = Picture.get\_most\_viewed\_picture  
end  
end

In the view (/app/views/users/index.html.erb):

## Most viewed picture:
\<%= image\_tag @picture.url %\>

Should do the trick. If not, please explain a little more.

🙂 Lasse

2010/2/14 Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 14, 2010, 10:38pm UTC](https://www.ruby-forum.com/t/sharing-a-method-model-module-or/183440/4 "2010-02-14T22:38:18Z")

</div>

On Sun, 2010-02-14 at 17:06 +0100, Lasse B. wrote:

> end
> 
> In the view (/app/views/users/index.html.erb):
> 
> ## Most viewed picture:
> \<%= image\_tag @picture.url %\>
> 
> Should do the trick. If not, please explain a little more.

* * *

pretty much did the trick and in the process, I learned about ‘class’  
and ‘instance’ methods in the model so thanks very much - that was  
important for me to finally understand. I didn’t know the exact language  
for what I was looking for so I couldn’t find what I wanted on Google.  
Once I did…

[http://railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/](http://railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/)

This was extremely valuable to me.

The Controller code was something I am trying to avoid.

Thanks

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.
