1

I am unsure of the best way to approach this scenario and would appreciate some direction. Essentially I have a scenario whereby I need to record assets for an organisation.

There are various types of assets so the attributes differ from type to type however there are a number of fields common to all assets such as:

location
make
model
colour
purchase_date
warranty_period

Similar to: How to design a product table for many kinds of product where each product has many parameters

I had through of creating this as

one-to_many between Organisation and Asset
polymorhpic between Asset and Details

class Organisation < ApplicationRecord
  has_many :assets
end

class Asset < ApplicationRecord
  belongs_to :organisation
  belongs to :detail, polymorphic: true
end

class CarAsset < ApplicationRecord
  has_one :asset, as: :detail
end

class ComputerAsset < ApplicationRecord
  has_one :asset, as: :detail
end

My question is: I wish to create the asset & detail in a single form action so the user after selecting the asset type makes a single form entry for both models.

The user would click a link on the organisation show page:

<%= link_to "New car asset", new_organisation_asset_path(@organisation, query: :new_car_asset) %>

The in my controller I could do something similar to:

class AssetsController < ApplicationController
  def new
    @organisation = Organisation.find(params["organisation_id"])
    @asset  = @organisation.assets.new

    case params[:query]
      when "new_car_asset"
        @details = @asset.car_assets.new
      when "new_computer_asset"
        @details = @asset.computer_assets.new
    end
  end
end

In my view I could also check the value of params[:query] and render the corresponding form partial that relates to the asset type.

Is this going down the correct path or is there a better way to achieve this? It does feel quite clunky.

Dercni
  • 1,216
  • 3
  • 18
  • 38

1 Answers1

0

I think it would be maybe better to use has_many :trough, should get more out of it in a long run. Like this:

class Organisation < ApplicationRecord
  has_many :cars, through: assets
  has_many :cumputers, through: assets
  has_many :locations, through: assets
  has_many :purchase_date, through: assets
end

class Asset < ApplicationRecord
  belongs_to :organisation
  belongs_to :cars
  belongs_to :cumputers
  belongs_to any :locations
  belongs_to :purchase_date
end

class Car < ApplicationRecord
  has_one :organisation, through: assets
end

class Cumputer < ApplicationRecord
  has_one :organisation, through: assets
end

class Location < ApplicationRecord
  has_one :organisation, through: assets
end

class Purchase_date < ApplicationRecord
  has_one :organisation, through: assets
end

Then you can create Assets within Organisations_controller and can nest everything in Organisation form with fields_for. Assets model would contain references between Organisation and every single detail model, but everything would be sepperate if you wonna do more with it in views or special fields.

meshin
  • 478
  • 2
  • 8