我如何在创建之前检查名称是否存在于 Associations table 中

how do i check if name exists in Associations table before create

我有两个模型 ItemProm。 Item 有一列 prom 和 prom 有一列 promname.所以我希望仅当数据库中存在 prom 时才创建该项目。

example
Prom.exist?(promname: :prom) == true #create  
else
"i dont have that prom"
class Item < ApplicationRecord
 belongs_to :prom
end

class Prom < ApplicationRecord

end

我创建项目的视图

<%= form_with(model: @item, local: true,  html:  { autocomplete: "off" }) do |form| %>

  <% if @item.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2>

      <ul>
        <% @item.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <table class="content-table">
  <thead>
    <tr>
      <th>Item Name</th>
      <th>Prom Name</th>
      <th>Prom Code</th>
      <th>Price</th>
      <th>M.M</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>
    <tr>

    <% @items.each do |item| %>
      <tr>
        <td><%= item.itemName %></td>
        <td><%= item.prom %></td>
        <td><%= item.promCode %></td> 
        <td><%= item.price %></td>
        <td><%= item.monadaMe %></td>
        <td><%= link_to 'Edit', edit_item_path(item), class: 'action_button' %></td>
        <td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' }, class: 'action_button' %></td>
              </tr>
    <% end %>
  </tbody>
</table>
<table class="insert-to-items">
  <thead>
    <tr>
<th>Item Name</th>
      <th>Prom Name</th>
      <th>Prom Code</th>
      <th>Base Code</th>
      <th>Description</th>
      <th>Price</th>
      <th>ΦΠΑ %</th>
      <th>Οικογενεια</th>
      <th>M.M</th>
    </tr>
     </tr>
     <tbody>
    <tr>
        <td><input size="13" <%= form.text_field :itemName %></td>
        <td><input id="myInput" size="13" <%= form.text_field :prom %></td> 
        <td><input size="13" <%= form.text_field :promCode %></td>
        <td><input size="13" <%= form.text_field :baseCode %></td>
        <td><input size="13" <%= form.text_field :desc %></td>
        <td><input type="number" <%= form.number_field :price, step: '0.001' %></td>
        <td><input type="number" <%= form.number_field :fpa %></td>
        <td><input size="13" <%= form.text_field :familys %></td>
        <td><input size="13" <%= form.text_field :monadaMe %></td>
      </tr>
  </tbody>
  </thead>
</table>
<script>
var promList = <%= raw @autoComplete.to_json %>;
</script>
<div class="actions">
    <center><%= form.submit "Save", class: 'button-items' %></center>
     <%= javascript_pack_tag 'find_me' %>
  </div>
  <% end %>

示例:Prom.promname = 'Company' 存在于数据库中,当我想创建一个 Item.prom.promname ='Company' 时,它会创建它。但如果它丢失了,它就不会创建它。

我是 Ruby Rails 的新人,如果您需要更多与问题相关的信息,请随时询问,在此先感谢。

您可以为项目模型添加自定义验证。在保存它之前,它将 运行 并在满足您的条件时创建,或者只会像您在问题中写的那样给出错误。

class Item < ApplicationRecord
  belongs_to :prom

  validate :validate_prom_exist

  def validate_prom_exist
    prom = Prom.where(promname: self.prom.promname)
    if prom.blank?
      errors.add(:item, 'i dont have that prom')
    end
  end
end