dimanche 12 juin 2022

Associated in 3 table rails

I have to create 3rd table having assosisted with 2nd.

1st table: Schema
2nd table: Entity 
3rd table: Fields

Relation between them is:

Schema(id) ===>  Entity
Entity(id) ===> Fields

I have created table and views till Entity but don't know how to create table for field

Fields having values as:

"clientId"
"name"
"description"
"mnemonic"
"columnType"
"display"
"fallbackId"
"conceptDisplay"
"businessName"

Help me to create model and views for it to show all the fiels related to entity:

I tried something like this:

entity.rb => model

class Entity < ApplicationRecord
  belongs_to :schema
  has_many :fields, dependent: :destroy
end

field.rb => model

class Field < ApplicationRecord
  belongs_to :entity
  belongs_to :schema
end

schema.rb => model

class Schema < ApplicationRecord
  has_many :entities, dependent: :destroy

  validates :name, presence: true, uniqueness: true
  validates :schemaId, presence: true, uniqueness: true
end

controller for field:

class FieldsController < ApplicationController
  def index
    @schema =  Schema.find(params[:id])
    @entity =  @schema.entities.find_by(id: params[:id])
    @fields =  @entity.fields
  end
end

views for entity where i have to create link_to to show all field for that entity

<div class="content">
  <ul>
  <% @entities.each do |data| %>
    <li>
      <%= data.clientId %>
      <%= link_to data.name, fields_index_path(id: data.id) %>
      <%= data.description %>
      <%= data.mnemonic %>
    </li>

  <% end %>
</ul>
</div>

Aucun commentaire:

Enregistrer un commentaire