user3075049
user3075049

Reputation: 1431

Rails_admin configuration for polymorphic association

I have Room, Home and Work models as following:

class Home < ActiveRecord::Base
  has_many :rooms, as: :available_room
end

class Work < ActiveRecord::Base
  has_many :rooms, as: :available_room
end

class Room < ActiveRecord::Base
  belongs_to :available_room, polymorphic: true
end

The migration of Room looks as following

class CreateRoom < ActiveRecord::Migration
  def change
    create_table :rooms do |t|
      t.integer :area

      t.references :available_room, polymorphic: true

      t.timestamps
    end
  end
end

Now, I want to configure the Home model, using RailsAdmin, and be able to specify a number of available rooms as well as areas.

How can do it via rails_admin in the model Home?

Thanks!

Upvotes: 2

Views: 2574

Answers (1)

marvelousNinja
marvelousNinja

Reputation: 1510

class Room < ActiveRecord::Base
  belongs_to :available_room, :polymorphic => true, :inverse_of => :rooms
end

class Home < ActiveRecord::Base
  has_many :rooms, :as => :available_room, :inverse_of => :available_room

  rails_admin do
    field :rooms
  end
end

Try something like this. RailsAdmin should render a pretty widget for adding new records.

Upvotes: 3

Related Questions