title: Ruby: object relation
body: <p>I am working on a project involving object relations, and building classes that interact with each other through association. </p>
  
  <p>Specifically, I am having problems with the .find_or_create_by_name method. This method finds or creates an artist by name, maintaining uniqueness of objects by name property. This works. 
  The next part is it should create a new instance of Artist if none exists. This doesn't work. </p>
  
  <pre><code>class Artist
    attr_accessor :name, :song
    @@all = []
  
    def initialize(name)
      @name = name
      @songs = []
    end
  
    def self.name=
      @name
    end
  
    def add_song(song)
      @songs &lt;&lt; song
    end
  
    def songs
      @songs
    end
  
    def self.all
      @@all
    end
  
    def save
      self.class.all &lt;&lt; self
    end
  
    def self.new_by_name(name)
      artist_name = Song.new(name)
      artist_name
    end
  
    def self.create_by_name(name = nil)
      artist = self.new_by_name(name)
      artist.save
      artist
    end
  
    def self.find_by_name(name = nil)
      @@all.detect{|x| x.name == name}
    end
  
    def self.find_or_create_by_name(name)
      if find_by_name(name) == nil
        create_by_name(name)
      else
        find_by_name(name)
      end
    end
  
  end
  </code></pre>