Home Rails How to access different DB table in a view
Reply: 1

Rails How to access different DB table in a view

Jesse Bell
1#
Jesse Bell Published in 2018-02-14 01:11:37Z

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
Sebastian Palma
2#
Sebastian Palma Reply to 2018-02-14 01:20:35Z

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306082 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO