2017-08-21 4 views
1

このシナリオにアプローチする最善の方法は不明であり、ある方向に感謝します。基本的に私は、組織の資産を記録する必要があるシナリオを持っています。多態性、STIなど

location 
make 
model 
colour 
purchase_date 
warranty_period 

同様に: How to design a product table for many kinds of product where each product has many parameters

私が持っていた属性が入力するタイプと異なるよう

は、しかし、のようなすべての資産に共通するフィールドの数がある資産の様々な種類があります。

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 

としてこれを作成するを通じて私の質問は: 資産タイプ&を単一のフォームアクションで作成したいので、ユーザーが資産タイプを選択した後、両方のモデルに対して単一のフォームエントリを作成します。

、ユーザーが組織のショーのページのリンクをクリックします:私も値をチェックすることができ、私の見解では

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 

:私のコントローラで

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

ザ・を私は次のように何かを行うことができますparams [:query]を呼び出して、アセットタイプに関連する対応するフォームパーシャルをレンダリングします。

これは正しいパスになるか、これを実現する良い方法がありますか?それは非常にclunky感じる。

答えて

0

has_many :troughを使用する方がいいかもしれないと思いますが、長期的にはそれ以上のものが必要です。

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 

次にあなたがfields_forと組織の形でOrganisations_controllerと缶巣すべて内の資産を作成することができます:このように。アセットモデルには、組織と個々の詳細モデルの間の参照が含まれますが、ビューや特殊フィールドでそれ以上のことをすると、すべてがセパレートになります。