Q: RoR: Model referencing itself multiple times I have a Room model. Each room has 6 exits (north, east, south, west, up, down), and I should be able to do things like Room.first.nr to get the room to the north of the first one. The model I crafted is the following:
class Room < ApplicationRecord

  has_one :nr, class_name: 'Room', foreign_key: :id
  belongs_to :sr, class_name: 'Room'

  has_one :er, class_name: 'Room', foreign_key: :id
  belongs_to :wr, class_name: 'Room'

  has_one :sr, class_name: 'Room', foreign_key: :id
  belongs_to :nr, class_name: 'Room'

  has_one :wr, class_name: 'Room', foreign_key: :id
  belongs_to :er, class_name: 'Room'

  has_one :ur, class_name: 'Room', foreign_key: :id
  belongs_to :dr, class_name: 'Room'

  has_one :dr, class_name: 'Room', foreign_key: :id
  belongs_to :ur, class_name: 'Room'

end

However, when doing this in rails c
Room.create!(title:'sometitle', description:'somedescription', er: Room.first)

I'm getting this:
ActiveRecord::RecordInvalid: Validation failed: Sr must exist, Wr must exist, Nr must exist, Dr must exist, Ur must exist

I've been playing with inverse_of too to no avail.
Here is my migration:
class CreateRooms < ActiveRecord::Migration[5.0]
  def change
    create_table :rooms do |t|
      t.string :title, null: false
      t.text :description, null: false

      t.integer :nr_id, null: true
      t.integer :er_id, null: true
      t.integer :sr_id, null: true
      t.integer :wr_id, null: true
      t.integer :ur_id, null: true
      t.integer :dr_id, null: true

      t.timestamps
    end

    add_index :rooms, :nr_id
    add_index :rooms, :er_id
    add_index :rooms, :sr_id
    add_index :rooms, :wr_id
    add_index :rooms, :ur_id
    add_index :rooms, :dr_id

  end
end


A: You have conflicting association names (i.e. has_one :nr and belongs_to :nr). Should be something like the following:
class Room < ApplicationRecord

  belongs_to :sr, class_name: 'Room'
  belongs_to :wr, class_name: 'Room'
  belongs_to :nr, class_name: 'Room'
  belongs_to :er, class_name: 'Room'
  belongs_to :dr, class_name: 'Room'
  belongs_to :ur, class_name: 'Room'

end

I do not think you would still need the has_one relationships. Because you can already get the rooms in all directions with just these belongs_to

A: Okay, took a while but I figured it out.
The model takes this form:
class Room < ApplicationRecord

  has_one :nr, class_name: 'Room', foreign_key: 'sr_id', inverse_of: :sr

  has_one :er, class_name: 'Room', foreign_key: 'wr_id', inverse_of: :wr

  has_one :sr, class_name: 'Room', foreign_key: 'nr_id', inverse_of: :nr

  has_one :wr, class_name: 'Room', foreign_key: 'er_id', inverse_of: :er

  has_one :ur, class_name: 'Room', foreign_key: 'dr_id', inverse_of: :dr

  has_one :dr, class_name: 'Room', foreign_key: 'ur_id', inverse_of: :ur

end

So I can do
a=Room.create(title:'the title', description: 'the description')
a.er = Room.find(1)
a.save

and this holds true:
Room.find(1).wr == Room.last

Thanks anyway @Jay-Ar Polidario
