SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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.

  2. #2
    SitePoint Enthusiast TomK32's Avatar
    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.
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  3. #3
    SitePoint Evangelist
    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
    Then alter the process that populates your system to this:
    Code:
    song = Song.new
    song.source = item.elements['location'].text
    song.song = item.elements['title'].text
    song.artist = item.elements['creator'].text
    @songs << song
    Then the code you want to use will work.

  4. #4
    SitePoint Evangelist
    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
    Create a table called songs with the required fields. For example via migration
    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
    And then change the code to this:
    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

  5. #5
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by ReggieB View Post
    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
    Then alter the process that populates your system to this:
    Code:
    song = Song.new
    song.source = item.elements['location'].text
    song.song = item.elements['title'].text
    song.artist = item.elements['creator'].text
    @songs << song
    Then the code you want to use will work.
    Thanks man that was what I was looking for. Really helps out appreciate it.
    Have a good day.

  6. #6
    SitePoint Evangelist
    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.

  7. #7
    SitePoint Wizard Darren884's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •