Jesse Bell
Jesse Bell

Reputation: 123

Rails How to access different DB table in a view

I am new to Ruby and RubyOnRails. I have been following a basic Rails tutorial thus far and have created several views and controllers to be able to perform basic CRUD on my MySQL DB, each one is specific to tables in the DB.

I started a new view, and I want to be able to display info from two separate tables. I want to be able to grab the Customers Name for their contract. I feel like this is an easy and common fix, What the heck am I overlooking?

Contract View

 <table>
    <tr>
    <th>Contract ID</th>
    <th>Customer ID</th>
    <th>Discount</th>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Payment Terms</th>
    <th>Delivery Day Of Week</th>
    <th>Employee ID</th>
    <th>Note</th>
    <th>Commission</th>
    <th>Active</th>

    </tr>

    <% @contracts.each do |contract| %>
    <tr>
    <td><%= contract.ContractID %></td>
    <td><%= contract.CustomerID %></td>
    <td><%= contract.fields_for :customer do |w| %>
    <%= w.text_field :CustomerName %>
    <% end %>
    </td>
    <td><%= contract.Discount %></td>
    <td><%= contract.StartDate %></td>
    <td><%= contract.EndDate %></td>
    <td><%= contract.PaymentTerms %></td>
    <td><%= contract.DeliveryDayOfWeek %></td>
    <td><%= contract.EmployeeID %></td>
    <td><%= contract.Note %></td>
    <td><%= contract.Commission %></td>
    <td><%= contract.Active %></td>


    </tr>
    <% end %>
    </table>

Contract Model

> class Contract < ApplicationRecord belongs_to :customer
> 
> accepts_nested_attributes_for :customer
> #Validation
>   
> 
> #Mapping this object to the Database tables self.table_name = "contract" self.primary_key = "ContractID" end

Customer Model

> class Customer < ApplicationRecord
> 
> has_many :contracts
> 
> #Validation validates :CustomerID, :CustomerTypeID, presence: true
> 
> validates :CustomerID, uniqueness: true
> 
> #Mapping this object to the Database tables self.table_name = "customer" self.primary_key = "CustomerID"
> 
> end

Contracts Controller

class ContractsController < ApplicationController
  def index
    @contracts = Contract.all
    @customers = Customer.all
  end
end

Upvotes: 1

Views: 172

Answers (1)

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33471

Since you've already defined that a Customer may have many contracts then you need now to define that that Contract belongs to a customer, so, modifying your models they should look like:

class Contract < ApplicationRecord
  belongs_to :customer
  ...
end

class Customer < ApplicationRecord
  has_many :contracts
  ...
end

Now you're able to access the customer name attribute from a certain contract, like:

<% @contracts.each do |contract| %>
  <%= contract.customer.name %>
<% end %>

This should work on getting the customer name, but for improving that query, you might add an includes on the index action:

def index
  @contracts = Contract.includes(:customer).all
  ...
end

Upvotes: 3

Related Questions