How to access and present similar polymorphic models in the same form in Rails?

Suppose I have 3 models Car, Motorcycleand Truck, for each, I must introduce a lot of things, such as a list of known previous owners, traffic tickets, license plates, etc. Therefore, I created a model for each ( PreviousOwners, PreviousPlatesetc.) and established polymorphic associations for related models.

The problem is, how can I introduce all this using only one form, sort of like:

Car #123

Known previous owners:
Jason Jazz
Brian Bass [add another]

Known previous license plates:
12345
67890 [add another]

Current status:
Cleared 
(this is a dropdown select menu, CurrentStatus is also a polymorphic association, but with predefined values.)

etc

This proves that this is a bitch far surpassing my level of knowledge (newbie here). Resources are not nested, and almost everything I find on several models is for nested resources, and nothing seems to apply to polymorphic associations.

( , , Vehicle "Car" .. , .)

.

+3
6
+2

Rails 2.3, , , . , , , . , Ryans Scraps, : Superslau ( )

. , !

 class Task < ActiveRecord::Base
   has_many :assets, :dependent=>:destroy
   accepts_nested_attributes_for :assets, :allow_destroy => true
   belongs_to :workable, :polymorphic => true
 end



 class Upload < ActiveRecord::Base
   has_one :task, :as => :workable, :dependent=>:destroy
   accepts_nested_attributes_for :task, :allow_destroy => true 
 end

- . .

, , . .

:

 def new   
  @upload = Upload.new  
  @upload.task = Task.new  
  @upload.task.assets.build 
 end 

Dont , , accepts_nested_attributes_for . Eloy!

+2

, AFAIK. ?

+1
+1

// , . , STI ( ).

But yes, you first need to see your models before I can give you the code.

+1
source

You can avoid this and make things a little easier by introducing a vehicle model. A car model can have all of your previous, previous, etc. Collections, then your truck, automobile, and motorcycle models may have a has_one vehicle.

+1
source

Source: https://habr.com/ru/post/1711152/