Robin Heggelund Hansen
Robin Heggelund Hansen

Reputation: 5006

ActiveAdmin form with double nesting

I have the following models:

class Price < ActiveRecord::Base
  has_many :meta_price_informations
  has_many :zone_price_informations, through: :meta_price_informations
  has_many :general_price_informations, through: :meta_price_informations

  accepts_nested_attributes_for :meta_price_informations
end

class MetaPriceInformation < ActiveRecord::Base
  belongs_to :price
  has_one :general_price_information
  has_one :zone_price_information

  accepts_nested_attributes_for :general_price_information, :zone_price_information
end

class ZonePriceInformation < ActiveRecord::Base
  belongs_to :meta_price_information

  validates :label, length: { in: 3..50 }
end

class GeneralPriceInformation < ActiveRecord::Base
  belongs_to :meta_price_information

  validates :label, length: { in: 3..5a0 }
  validates :helper, length: { in: 0..50 }
end

Now I want to be able to create a price model in active-admin and add as many ZonePriceInformations and GeneralPriceInformations as I'd like, with the option of setting the position for each of them.

form do |f|
  f.inputs do
    f.input :valid_from
  end
  f.inputs "Sone baserte priser" do
    f.has_many :meta_price_informations, allow_destroy: true, new_record: true do |m|
      m.inputs do
        m.input :position
        m.semantic_fields_for :zone_price_information do |zone|
          zone.input :label
        end
      end 
    end
  end

This doesn't work. I've also tried a m.inputsinstead of m.semantic_fields_for to no effect. Also tried m.inputs :label, for: :zone_price_information which also doesn't do anything.

Anyone know what I can do?

Upvotes: 0

Views: 592

Answers (1)

nistvan
nistvan

Reputation: 2960

In the f.has_many block unfortunately you can only use one model, that joins to Price model directly. So you have to create a model that contains all the attributes that you want to set in your has_many form. And that model should associate to Price with has_many.

Upvotes: 1

Related Questions