# Where to put db-less objects?

**URL:** <https://www.ruby-forum.com/t/where-to-put-db-less-objects/69217>\
**Category:** Rails\
**Created:** [September 19, 2006, 1:32pm UTC](https://www.ruby-forum.com/t/where-to-put-db-less-objects/69217 "2006-09-19T13:32:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecd19e/32.png) [@harp](https://www.ruby-forum.com/u/harp)\
**Post date:** [September 19, 2006, 1:32pm UTC](https://www.ruby-forum.com/t/where-to-put-db-less-objects/69217/1 "2006-09-19T13:32:59Z")

</div>

Hi,

thanks in adavance for any help/insights.  
i have just finished creating a Magazine model class, that is it’s own  
little object, not backed by a db. altogether i have 20 magazines or so,  
and they won’t change throughout the application, so there is no need  
for a table to back these up. hence, i created the following:

class Magazine

attr\_accessor …

def initialize(mag\_id, title, description)  
@mag\_id = mag\_id  
@title = title  
@description = description  
end

end

mag0 = Newsletter.new(0, ‘cosmo’, ‘description of the cosmo mag goes  
here’)  
mag1 = Newsletter.new(1, ‘pc magazine’, 'description of the pcs and all  
the computers and stuff goes here, ya ')  
mag2 = Newsletter.new(2, ‘road trips are good’, ‘description of other  
trips’)  
mag3 = Newsletter.new(3, ‘blahs and yas’, 'description of the pcs and  
all the computers and stuff goes here, ya ')  
mag4 = Newsletter.new(4, ‘moo papa’, ‘gulu is a article sombrero’)

the above objects work fine when i put them straight into the rhtml i  
want to display them, but i was hoping that there was a conventional  
place that i’m not aware of on where to put these, as when i put them in  
the model, the app didn’t recognize these objects.

i can’t put this in enviornment.rb, although for some reason, i feel as  
if i should…how should i go about this issue?

many thanks,

harp

---

<div class="post-metadata">

**Author:** ![harp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecd19e/32.png) [@harp](https://www.ruby-forum.com/u/harp)\
**Post date:** [September 19, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/where-to-put-db-less-objects/69217/2 "2006-09-19T13:53:28Z")

</div>

harp wrote:

> Hi,
> 
> thanks in adavance for any help/insights.  
> i have just finished creating a Magazine model class, that is it’s own  
> little object, not backed by a db. altogether i have 20 magazines or so,  
> and they won’t change throughout the application, so there is no need  
> for a table to back these up. hence, i created the following:
> 
> class Magazine
> 
> attr\_accessor …
> 
> def initialize(mag\_id, title, description)  
> @mag\_id = mag\_id  
> @title = title  
> @description = description  
> end
> 
> end
> 
> mag0 = Newsletter.new(0, ‘cosmo’, ‘description of the cosmo mag goes  
> here’)  
> mag1 = Newsletter.new(1, ‘pc magazine’, 'description of the pcs and all  
> the computers and stuff goes here, ya ')  
> mag2 = Newsletter.new(2, ‘road trips are good’, ‘description of other  
> trips’)  
> mag3 = Newsletter.new(3, ‘blahs and yas’, 'description of the pcs and  
> all the computers and stuff goes here, ya ')  
> mag4 = Newsletter.new(4, ‘moo papa’, ‘gulu is a article sombrero’)
> 
> the above objects work fine when i put them straight into the rhtml i  
> want to display them, but i was hoping that there was a conventional  
> place that i’m not aware of on where to put these, as when i put them in  
> the model, the app didn’t recognize these objects.
> 
> i can’t put this in enviornment.rb, although for some reason, i feel as  
> if i should…how should i go about this issue?
> 
> many thanks,
> 
> harp

I’m assuming that the code you’ve shown above is all in one file. If  
this is the case then those mag0,1,2,4 variables are only scoped to that  
file. Putting the Magazine class file in the models directory is the  
right thing to do. You could try…

class Magazine

def self.all\_magazines

end

mag0 = Newsletter.new(0, ‘cosmo’, ‘description of the cosmo mag goes  
here’)  
mag1 = Newsletter.new(1, ‘pc magazine’, 'description of the pcs and all  
the computers and stuff goes here, ya ')  
mag2 = Newsletter.new(2, ‘road trips are good’, ‘description of other  
trips’)  
mag3 = Newsletter.new(3, ‘blahs and yas’, 'description of the pcs and  
all the computers and stuff goes here, ya ')  
mag4 = Newsletter.new(4, ‘moo papa’, ‘gulu is a article sombrero’)

---

<div class="post-metadata">

**Author:** ![harp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecd19e/32.png) [@harp](https://www.ruby-forum.com/u/harp)\
**Post date:** [September 19, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/where-to-put-db-less-objects/69217/3 "2006-09-19T14:00:22Z")

</div>

harp wrote:

> Hi,
> 
> thanks in adavance for any help/insights.  
> i have just finished creating a Magazine model class, that is it’s own  
> little object, not backed by a db. altogether i have 20 magazines or so,  
> and they won’t change throughout the application, so there is no need  
> for a table to back these up. hence, i created the following:
> 
> class Magazine
> 
> attr\_accessor …
> 
> def initialize(mag\_id, title, description)  
> @mag\_id = mag\_id  
> @title = title  
> @description = description  
> end
> 
> end
> 
> mag0 = Newsletter.new(0, ‘cosmo’, ‘description of the cosmo mag goes  
> here’)  
> mag1 = Newsletter.new(1, ‘pc magazine’, 'description of the pcs and all  
> the computers and stuff goes here, ya ')  
> mag2 = Newsletter.new(2, ‘road trips are good’, ‘description of other  
> trips’)  
> mag3 = Newsletter.new(3, ‘blahs and yas’, 'description of the pcs and  
> all the computers and stuff goes here, ya ')  
> mag4 = Newsletter.new(4, ‘moo papa’, ‘gulu is a article sombrero’)
> 
> the above objects work fine when i put them straight into the rhtml i  
> want to display them, but i was hoping that there was a conventional  
> place that i’m not aware of on where to put these, as when i put them in  
> the model, the app didn’t recognize these objects.
> 
> i can’t put this in enviornment.rb, although for some reason, i feel as  
> if i should…how should i go about this issue?
> 
> many thanks,
> 
> harp

Objects you want available in the view should be set up in the  
controller action. Having a model file for the Magazine object is the  
right way to go. Once you’ve got it you can do this…

class Magazine  
def self.find\_all  
# this method returns an array of all magazines, if that’s what you  
want.  
end  
end

Then in the controller action where you want to be able to use the  
magazines in the view…

def list\_magazines  
@magazines = Magazines.find\_all

# The rest of the controller code goes here.

end

And in the view you can do…

\<% @magazines.each do |magazine| %\>  
\<%= magazine.title %\>  
\<% end %\>

or whatever else you need. I’m still learning this stuff myself, so  
apologies for any mistakes or misunderstandings.

---

<div class="post-metadata">

**Author:** ![harp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecd19e/32.png) [@harp](https://www.ruby-forum.com/u/harp)\
**Post date:** [September 19, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/where-to-put-db-less-objects/69217/4 "2006-09-19T17:41:09Z")

</div>

perfecto.  
thanks.

harp
