Rails - complex model/associations -


in app building learn rails (rails 5), have following end result try in place:

i want users able tag content of pdf (attached either annotation record or document record). after annotation or document pdf attached has been classified document type (e.g. po, delivery note, etc.). tag, when added, associated predefined list of tag types (matching document type of annotation respectively of document document type of tag type). when adding tag, want capture content tagged (e.g. po number or orderer name) , coordinates in pdf.

first part of question

2 objects (classes / models) annotation , document have 1-to-many relationship object (class / model) tag (similar order order_item):

has_many :tags, dependent: :destroy 

the model tag has 1-to-1 relationship model tagtype:

belongs_to :annotation, :document 

one single tag-record however, can belong 1 annotation / document , needs deleted when respective annotation / document gets deleted (i set dependent: :destroy that).

so, type of association use tag tagtype? has_one? has_many? belongs_to_...?

second part of question:

now, when adding tag annotation or document, tag ge_ extracted text, coordinates , needs assigned tagtype. however, tag types (for document type of annotation or document) can used once annotation / document - depending on tag_type field "multiple occurrence" false. how / set (validation / filter) in association?

and how reduce list depending on tags in place (dynamically)?

all suggestions / directions welcome!

for first part:

for association between tag , tag_type(tagtype) model, can use belongs_to in 1 model , has_one in other. example, tag model can have belongs_to :tag_type, inverse_of: :tag , tagtype model can have has_one :tag, inverse_of: :tag_type. model uses belongs_to , uses has_one association depend on location of foreign key. example, above code work if tags table has tag_type_id column (the model belongs_to association should contain id of other table; @ answer: https://stackoverflow.com/a/3808936/6006050).

for second part:

i'm assuming there no direct association between tagtype model , annotation or document, , there association between tag , annotation/document models, , between tag , tagtype models. again, validation should in model contains foreign key in association between tag , annotation/document. since association between annotation/document , tag models has_many, i'm assuming tags table hold foreign key. in case, validation should in tag model when associate annotation/document tag, validation fire.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -