SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Making models for hashes?
-
Mar 11, 2009, 15:19 #1
- Join Date
- Aug 2003
- Location
- Southern California, United States
- Posts
- 1,616
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Making models for hashes?
I have an XML parser and I am assigning it like this:
Code Ruby:song = {} song['source'] = item.elements['location'].text song['song'] = item.elements['title'].text song['artist'] = item.elements['creator'].text @songs << song
It attaches itself onto songs which is done like:
@songs = []
In my view I am displaying it like:
Code Ruby:<% @songs.each do |song| %> <%= song['artist'] %> <% end>
My question is how do I make it so its a model instead of displaying it improperly like that on the view page? How do I make it so I can do
Code Ruby:<% for song in @songs %> <%= song.artist %> <% end >
instead of what I have now? Thank you for the help.Have a good day.
-
Mar 12, 2009, 05:14 #2
- Join Date
- Mar 2008
- Location
- Berchtesgaden, Germany
- Posts
- 32
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You're talking about "model", are you planning to use this song model in Ruby on Rails? Cuz then you just define it in your database, have a simple class file and then you can use it that way.
If you don't, stick with the normal Hash notation as it's the most simple. You could use symbols instead of strings for the hash key if you want to save a few bytes of memory.
-
Mar 12, 2009, 09:41 #3
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you are not going to store the data in a database, you need to create a standalone class to store the data. So create a new class call Song:
create a file lib/song.rb that looks like this:
Code:class Song attr_accessor :source, :song, :artist end
Code:song = Song.new song.source = item.elements['location'].text song.song = item.elements['title'].text song.artist = item.elements['creator'].text @songs << song
-
Mar 12, 2009, 09:52 #4
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
On the other hand, if you do want to store the data in a database, create a new model called Song:
Code:class Song < ActiveRecord::Base end
Code:class CreateSongs < ActiveRecord::Migration def self.up create_table :songs do |t| t.column :status, :string t.column :absence, :string t.column :lone_working_note, :string t.timestamps end end def self.down drop_table :songs end end
Code:song = Song.new song.source = item.elements['location'].text song.song = item.elements['title'].text song.artist = item.elements['creator'].text song.save @songs << song
-
Mar 12, 2009, 13:51 #5
-
Mar 13, 2009, 04:49 #6
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You're welcome.
By the way. When I said "If you are not going to store the data in a database, you need to create a standalone class to store the data." what I should have said was "to hold the data" rather than "to store the data". The data held in the objects will be lost when the object is killed (for example by the Ruby garbage disposal at the end of each complete process). I presume you intend displaying data stored as XML files, in which case this will be fine.
-
Mar 13, 2009, 09:25 #7
- Join Date
- Aug 2003
- Location
- Southern California, United States
- Posts
- 1,616
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, I only wanted to display the data, there is a model that handles storing it separately. I remember reading about making a class with attr_accessor but I probably was storing it somewhere other than /lib. Thanks again.
Have a good day.
Bookmarks