Reputation: 745
In my app that I am building to learn rails (RAILS 5), I have following end result I try to get in place:
I want users to be able to tag content of a PDF (attached to either an annotation record or a document record). After the annotation or document that the PDF is attached to has been classified by document type (e.g. PO, delivery note, etc.). A tag, when added, is associated with a predefined list of tag types (matching the document type of the annotation respectively of the document with the document type of the tag type). When adding the tag, I want to capture the content tagged (e.g. PO number or orderer name) and the coordinates in the PDF.
2 objects (classes / models) Annotation and Document have a 1-to-many relationship with the object (class / model) Tag (similar to order to order_item):
has_many :tags, dependent: :destroy
The model Tag has a 1-to-1 relationship with the model TagType:
belongs_to :annotation, :document
One single Tag-record however, can only belong to one Annotation / Document and needs to be deleted when the respective Annotation / Document gets deleted (I set dependent: :destroy
for that).
So, which type of association to use for Tag with TagType? has_one
? has_many
? belongs_to_
...?
Now, when adding a Tag to an Annotation or Document, the Tag will ge_ extracted text, coordinates and a needs to be assigned to a TagType. However, some tag types (for the document type of the annotation or document) can only be used once for an annotation / document - depending on the tag_type field "multiple occurrence" is false. How / where do I set this (validation / filter) up in the association?
And how to reduce the list depending on the tags in place (dynamically)?
All suggestions / directions welcome!
Upvotes: 1
Views: 216
Reputation: 392
For the association between Tag and Tag_type(TagType) model, you can use belongs_to in one model and has_one in the other. For example, Tag model can have belongs_to :tag_type, inverse_of: :tag
and TagType model can have has_one :tag, inverse_of: :tag_type
. Which model uses the belongs_to and which uses the has_one association will depend on the location of the foreign key. For example, the above code will work if the tags table has a tag_type_id column (The model with the belongs_to association should contain the id of the other table; look at this answer: https://stackoverflow.com/a/3808936/6006050).
I'm assuming there is no direct association between TagType model and Annotation or Document, and there is an association between Tag and Annotation/Document models, and between Tag and TagType models. Again, the validation should be in the model that contains the foreign key in the association between Tag and Annotation/Document. Since the association between Annotation/Document and Tag models is has_many, I'm assuming the tags table will hold the foreign key. In that case, the validation should be in the Tag model so that when you associate an Annotation/Document to the tag, the validation will fire.
Upvotes: 1