Where to put db-less objects?

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

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’)

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.

perfecto.
thanks.

harp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs