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

    <th>Contract ID</th>
    <th>Customer ID</th>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Payment Terms</th>
    <th>Delivery Day Of Week</th>
    <th>Employee ID</th>


    <% @contracts.each do |contract| %>
    <td><%= contract.ContractID %></td>
    <td><%= contract.CustomerID %></td>
    <td><%= contract.fields_for :customer do |w| %>
    <%= w.text_field :CustomerName %>
    <% end %>
    <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>

    <% end %>

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
Sebastian Palma
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

class Customer < ApplicationRecord
  has_many :contracts

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