title: Save and get data in the same table by different relationships - Ruby on rails
body: <p>I have a table in my database called <code>place_events</code></p>
  
  <p>I need to save two types of places in that table, these types are, one <code>main</code> and some <code>alternate</code> (optional)</p>
  
  <p><a href="https://i.stack.imgur.com/vdf0l.png" rel="nofollow noreferrer">My view</a></p>
  
  <p>My table:</p>
  
  <pre><code>create_table "place_events", force: :cascade do |t|
      t.datetime "created_at",                   null: false
      t.datetime "updated_at",                   null: false
      t.integer  "event_place_id",   limit: 4
      t.string   "event_place_type", limit: 255
      t.integer  "event_id",         limit: 4
    end
  </code></pre>
  
  <p>I'm trying to save my data by different relationships so that later I can get them</p>
  
  <pre><code>if !params[:precint].nil?
        precint_hash = JSON.parse(params[:precint])
        precint_hash = precint_hash.except('id', 'description', '$$hashKey', 'logo')
        precint = PrecintEvent.new(precint_hash)
        @event.build_place_event(event_place: precint)
      end
  
      if params[:alternate_precints]
        precints_hash = JSON.parse(params[:alternate_precints])
        precints_hash.each do |precint|
          precint = precint.except('id', 'description', '$$hashKey', 'logo')
          precint = PrecintEvent.new(precint)
          @event.alternate_place_events.build(event_place: precint)
        end
      end
  </code></pre>
  
  <p>that code works and saves the data in my table <code>place_events</code> but my problem is when I want to get them, get the principal and get the alternates</p>
  
  <p>I try these relationships in my model <code>event.rb</code>:</p>
  
  <pre><code>has_one :place_event
  has_many :alternate_place_events, class_name: PlaceEvent.name, foreign_key: 'event_id'
  </code></pre>
  
  <p>but this does not work</p>
  
  <p>This is correct:</p>
  
  <pre><code>@event.place_event
  #&lt;PlaceEvent id: 8, created_at: "2019-06-14 00:27:50", updated_at: "2019-06-14 00:27:50", event_place_id: 8, event_place_type: "PrecintEvent", city_event_id: nil, event_id: 15&gt;
  </code></pre>
  
  <p>This is correct:
  I should not get the data with id number 8</p>
  
  <pre><code>@event.alternate_place_events
  
          #&lt;ActiveRecord::Associations::CollectionProxy [
      #&lt;PlaceEvent id: 8, created_at: "2019-06-14 00:27:50", updated_at: "2019-06-14 00:27:50", event_place_id: 8, event_place_type: "PrecintEvent", city_event_id: nil, event_id: 15&gt;, 
      #&lt;PlaceEvent id: 9, created_at: "2019-06-14 00:27:50", updated_at: "2019-06-14 00:27:50", event_place_id: 9, event_place_type: "PrecintEvent", city_event_id: nil, event_id: 15&gt;, 
      #&lt;PlaceEvent id: 10, created_at: "2019-06-14 00:27:50", updated_at: "2019-06-14 00:27:50", event_place_id: 10, event_place_type: "PrecintEvent", city_event_id: nil, event_id: 15&gt;]&gt;
  </code></pre>
  
  <p><strong>How could I do this? Is it possible to do it?</strong></p>