mercredi 19 septembre 2018

How to add queries between tables in Ruby on Rails

I'm trying to design a complicated form. Fields also need to add data that I received with queries to other tables.

Here's what I'm trying to do.

Rad_check form is composed of username and password... When the user registers by typing username and password, the Rad_cheks table must consist of other records in the back. I was able to do some of the Radcheck model. However, I want to conditionally query the tenant_id column in the Nas table and insert it into the Rad_checks table. I've actually prepared a query for that, but I don't know how to use it.

Na.select(:tenant_id).where(Na.arel_table[:realipaddr].eq('form's real ip will be'))

Actually, I'm using the Milia gem file. However, there will be a somewhat more public form... the query I created must come in a way instead of the IP address of the request. REMOTE_IP code. This means that the user's actual IP address is tenant_id information that is equal to the IP address in the NAS table.

Help me! Please

TABLE

class CreateRadChecks < ActiveRecord::Migration[5.2]
  def change
    create_table :rad_checks do |t|
      t.integer :tenant_id
      t.string :username
      t.string :password
      t.string :attribu
      t.string :op

      t.timestamps
    end
  end
end

FORM

<%= form_with(model: rad_check, local: true) do |form| %>
  <% if rad_check.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(rad_check.errors.count, "error") %> prohibited this rad_check from being saved:</h2>

      <ul>
      <% rad_check.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :username %>
    <%= form.text_field :username %>
  </div>

  <div class="field">
    <%= form.label :password %>
    <%= form.text_field :password %>
  </div>  

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

MODEL

class RadCheck < ApplicationRecord
    has_one :rad_user_group, dependent: :destroy

    after_initialize :add_rad_user_group
    before_save :set_radcheck   

    def add_rad_user_group
      self.rad_user_group ||= RadUserGroup.new if self.new_record?
    end 


    def set_radcheck
      self.rad_user_group.username = username
      self.op = ":="
      self.attribu = "Cleartext-Password"
    end
end

rad_user_group table

class CreateRadUserGroups < ActiveRecord::Migration[5.2]
  def change
    create_table :rad_user_groups do |t|
      t.integer :tenant_id
      t.string :username
      t.string :groupname
      t.references :rad_check, foreign_key: true

      t.timestamps
    end
  end
end

nas table

class CreateNas < ActiveRecord::Migration[5.2]
  def change
    create_table :nas do |t|
      t.integer :tenant_id
      t.string :nasname
      t.string :realipaddr
      t.boolean :active

    end
  end
end

Aucun commentaire:

Enregistrer un commentaire