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

Viewed 147

I have two models Item and Prom. Item have a column prom and prom have a column promname. So I want the item to be created only if prom exists in database.

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

class Prom < ApplicationRecord

end

and the view with I create the item with

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

Example: Prom.promname = 'Company' exists in database and when I wanted to create a Item.prom.promname ='Company' it will create it. But if it's missing it is not going to create it.

I'm new in Ruby on Rails and if you need more info related with question fell free to ask, thanks in advance.

1 Answers

You can add a custom validation for your Item model. Before saving it it will run and create if your conditions is met or just will give an error like you wrote in question.

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
Related