# How to use a class, put it inside a model?

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-class-put-it-inside-a-model/195834>\
**Category:** Rails\
**Created:** [September 30, 2010, 10:38pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-put-it-inside-a-model/195834 "2010-09-30T22:38:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [September 30, 2010, 10:38pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-put-it-inside-a-model/195834/1 "2010-09-30T22:38:27Z")

</div>

class Feeds  
FEEDS = %W(  
[http://feed.kennisnet.nl/Basisonderwijs](http://feed.kennisnet.nl/Basisonderwijs)  
[http://feed.kennisnet.nl/Voortgezetonderwijs](http://feed.kennisnet.nl/Voortgezetonderwijs)  
[http://wp.digischool.nl/nieuws/feed/](http://wp.digischool.nl/nieuws/feed/)  
)

def load\_feeds  
@feeds\_ds = feeds.first  
@feeds = vopo? ? feeds.second : feeds.last  
end

def feeds  
@feeds ||= Feedzirra::Feed.fetch\_and\_parse(FEEDS).values.map { |  
feed| feed.entries.first(6) }.entries  
end

def vopo?  
params[:vopo] == ‘po’  
end  
end

This is my class.  
I would like to display the feeds on every page. Where should i put  
it? in a model or controller?  
and how doe i use it?

def load\_feeds  
f = Feeds.new  
f::feeds  
f::load\_feeds  
end

This is what my view looks like

\<%- for feed in @feeds[0…2] -%\>  
\<%= link\_to feed.title, feed.url -%\>

\<%=h feed.summary[0..60] %\>...

\<%- end -%\>

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [September 30, 2010, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-put-it-inside-a-model/195834/2 "2010-09-30T22:52:30Z")

</div>

I would probably put the class in a file inside lib/ folder and add a  
helper  
for displaying the feeds.

\<%= feeds\_list %\>

Daniel GaytÃ¡n

2010/9/30 Lasse [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Lasse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/96bed5/32.png) [@Lasse](https://www.ruby-forum.com/u/Lasse)\
**Post date:** [October 1, 2010, 11:40am UTC](https://www.ruby-forum.com/t/how-to-use-a-class-put-it-inside-a-model/195834/3 "2010-10-01T11:40:15Z")

</div>

1. you should not put it into the any controller, controllers have  
another purpose, in controller you should only call feeds loading from  
some other class.
2. I agree to Daniel, you may put it into the lib/ folder or into app/  
models
3. The whether should you have a helper or not for rendering this part  
of the view depends on the whether will you call it from the different  
places of views (then you definitely should have a helper), or you’ll  
have only one place in say some main layout where you render it then  
there is no need in helper
4. If you want to see feeds within all your pages you should put the  
collecting method (load\_feeds) into some private/protected method of  
ApplicationController and then use it as before\_filter in your  
controllers (don’t forget to turn it off for e.g. destroy method when  
you don’t need it)
5. you should pass params[:vopo] to your class as some parameter since  
it’s part of controller
6. If you need just to fetch the feeds once per page I’d put the feeds  
loading into some static method, and later call it just like (my\_feeds  
= Feed.load\_feeds), if you need to use them in several places you may  
also use Singleton pattern ([Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/)  
Singleton\_pattern)
