在3台轨道上相关

发布于 2025-02-07 05:25:22 字数 1539 浏览 2 评论 0原文

我必须创建第三个表,并用第二个表进行了安装。

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

它们之间的关系是:

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

我已经创建了表格和视图直到实体,但不知道如何为

具有值的字段创建表:

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

帮助我创建模型和视图以显示与实体相关的所有fiels:

我尝试了一些东西像这样:

entity.rb =>型号

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

field.rb =&gt;模型

class Field < ApplicationRecord
  belongs_to :entity
end

schema.rb =&gt; 字段的模型

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

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

控制器:

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

实体的视图,我必须创建link_to以显示该实体的所有字段

<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>

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
end

schema.rb => model

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

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

controller for field:

class FieldsController < ApplicationController
  def index
    @field = Schema.find_by(id: params[:id]).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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强者自强 2025-02-14 05:25:22

您需要创建这些迁移:

  def change
    create_table :schemas do |t|
      # here all your necessary fields for schema
      t.timestamps
    end
  end
  def change
    create_table :entities do |t|
      t.references :schema, null: false, foreign_key: true
      # here all your necessary fields for entity
      t.timestamps
    end
  end

def change
  create_table :fields do |t|
    t.references :entity, null: false, foreign_key: true
    # here all your necessary fields for field
    t.timestamps
  end
end

并将所有必要的关联添加到模型中

# schema.rb
class Schema < ApplicationRecord
  has_many :entities
  has_many :fields, through: :entities
end
# entiry.rb
class Entity < ApplicationRecord
  belongs_to :schema
  has_many :fields
end
# field.rb
class Field < ApplicationRecord
  belongs_to :entiry
end

获取所有字段 for schema通过eNTITY在yout Controller中

# fields_controller.rb
class FieldsController < ApplicationController
  def index
    @fields = Schema.find_by(id: params[:id]).fields
  end
end

,并呈现所有链接到您的字段中的视图

# views/fields/index.html.erb
<% @fields.each do |field| %>
  <%= link_to field.name, field_path(field) %>
<% end %>

,并向您的路由添加必要的资源

# configs/routes
resources :schemas
resources :fields

You need to create these migrations:

  def change
    create_table :schemas do |t|
      # here all your necessary fields for schema
      t.timestamps
    end
  end
  def change
    create_table :entities do |t|
      t.references :schema, null: false, foreign_key: true
      # here all your necessary fields for entity
      t.timestamps
    end
  end

def change
  create_table :fields do |t|
    t.references :entity, null: false, foreign_key: true
    # here all your necessary fields for field
    t.timestamps
  end
end

And add all necessary associations to your models

# schema.rb
class Schema < ApplicationRecord
  has_many :entities
  has_many :fields, through: :entities
end
# entiry.rb
class Entity < ApplicationRecord
  belongs_to :schema
  has_many :fields
end
# field.rb
class Field < ApplicationRecord
  belongs_to :entiry
end

Fetch all fields for Schema through Entity in yout controller

# fields_controller.rb
class FieldsController < ApplicationController
  def index
    @fields = Schema.find_by(id: params[:id]).fields
  end
end

And render all links to your fields in view

# views/fields/index.html.erb
<% @fields.each do |field| %>
  <%= link_to field.name, field_path(field) %>
<% end %>

And add necessary resources to your routes

# configs/routes
resources :schemas
resources :fields
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文