I am getting the above error when trying to display the 'suburb' for all Vaults objects.
I have a Vault Model
class Vault < ActiveRecord::Base
belongs_to :user
has_one :address, dependent: :destroy
end
I have an Address Model
class Address < ActiveRecord::Base
belongs_to :vault
def to_s
"#{suburb}"
end
end
This is my index in my VaultsController
def index
#vaults = Vault.all
end
My Vaults view is as follows
<h1>Vaults</h1>
<table class="table">
<tr class="active">
<th>Location</th>
<th>Capacity</th>
</tr>
<% #vaults.each do |vault| %>
<tr>
<td><%= link_to vault.address.suburb, vault.address %></td>
<td><%= vault.capacity %></td>
</tr>
<%end%>
</table>
<%= link_to 'Create Vault', new_vault_path, class: 'btn btn-default btn-lg' %>
When I run the following command on the console it returns that correct field:
Vault.first.address.suburb
Related
I have these models
class Auto < ApplicationRecord
belongs_to :marca
belongs_to :modelo
belongs_to :cliente
end
class Cliente < ApplicationRecord
has_many :autos
end
class Marca < ApplicationRecord
has_many :modelos
has_many :autos
end
class Modelo < ApplicationRecord
belongs_to :marca
has_many :autos
end
and this index view
<table class="table table-striped" id="autos">
<tr>
<th>Id</th>
<th>Cliente</th>
<th>Marca</th>
<th>Modelo</th>
<th>Placas</th>
</tr>
<% #auto.each do |auto| %>
<tr>
<td><%= auto.id %></td>
<td><%= auto.cliente.nombre %></td>
<td><%= auto.marca.nombre%></td>
<td><%= auto.modelo.nombre %></td>
<td><%= auto.placas %></td>
</tr>
<% end %>
</table>
and this in my autos controller
def show
#auto = Auto.find(params[:id])
end
def index
#auto = Auto.all
end
the problem is that show me this error:
undefined method `nombre' for nil:NilClass in this line:
<td><%= auto.cliente.nombre %></td>
rarely at show view where I call
#auto.cliente.nombre
works fine, can you help me? thanks
It seems like <td><%= auto.cliente.nombre %></td> doesn't work because auto.cliente is nil, and you cannot call that method nombre on nil. Perhaps one of your auto objects doesn't have an associated client?
To see how this happens, try running nil.hello in Ruby and you should see a NoMethodError with the error message undefined method 'hello' for nil:NilClass.
auto.cliente.try(:nombre) will save you from error if you by chance allow auto without cliente.
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
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
I have a loop using associations. I'm looking to group by treatment and display in a row.
Each loop has three records for each treatment. The code below this is what i'm producing.
VIEW
<table>
<tr>
<td>Treatment</td>
<td>Date</td>
<td>Count</td>
</tr>
<% #trial.establishmentMethods.order(:treatment_selection_id).each do |data| %>
<tr>
<td><%= data.treatmentSelection.treatment.name %></td> This is reference by treatment_selection_id.
<td><%= data.date %></td>
<td><%= data.count %></td>
</tr>
<% end %>
</table>
This is what i'm hoping to produce. Display the treatment once, then loop the related treatment_selection_id's on the same row.
Here are my models and associations.
class Trial < ApplicationRecord
has_many :assessments, primary_key: 'trial_id'
has_many :establishmentMethods, through: :assessments
end
class EstablishmentMethod < ApplicationRecord
belongs_to :treatmentSelection, primary_key: 'treatment_selection_id', foreign_key: 'treatment_selection_id'
has_many :treatments, through: :treatmentSelection
end
class TreatmentSelection < ApplicationRecord
belongs_to :treatment, primary_key: 'treatment_id'
end
It seems like TreatmentSelection has_many establishmentMethods, so you should add that to the TreatmentSelection model. Then you can do something like:
<% treatment_selections.each do |treatment_selection| %>
<tr>
<td><%= treatment_selection.treatment.name %></td>
<% treatment_selection.establishmentMethods.each do |em| %>
<td><%= em.date %></td>
<td><%= em.count %></td>
<% end %>
</tr>
<% end %>
By the way, it's convention to use snake_case in ruby and it will make using associations easier.
Frnds I am new to rails, here i created two tables caleed stocks and stock_availabilities.
in the stock model
class Stock < ActiveRecord::Base
belongs_to :projects_lkp
has_many :stock_availabilities
validates_presence_of :item
end
In the stock_availabilities model
class StockAvailability < ActiveRecord::Base
belongs_to :stock
validates_presence_of :qty,:add_or_issue,:price, :captured_at, :stock_id,:unit
end
Now my doubt is how to bring the field of stock_availabilties in the views of stock
<% #stock.each do |d| %>
<tr>
<td><%= d.item %></td>
"Here i need to print the values of qty and pricevwhich is in stock_availabilities class"?
</tr>
You are on the right track.
this is what you need:
<% #stock.each do |d| %>
<tr>
<td><%= d.item %></td>
<% d.stock_availabilities.each do |sAV| %>
<td> <%= sAV.qty %> </td>
... <-- You do the other ones here
<% end %>
</tr>
I'm trying to reference a column from another table in my rails application. I want to reference the site_name using the matching site_id columns from both tables(sites and trials).
My controller
def list
#list = Trial.year(params[:year]).order(:region_id)
end
My Models
class Trial < ActiveRecord::Base
attr_accessible :trial_id, :site_id, :year, :trial_type, :region_id
scope :year, ->(year) { where(year: year) }
scope :trial_id, ->(trial_id) { where(trial_id: trial_id) }
belongs_to :site
end
class Site < ActiveRecord::Base
attr_accessible :site_id, :site_name, :region
has_many :trials
end
My View
<table class="table">
<th>Trial Location</th>
<th>Trial Type</th>
<th>Grower</th>
<% #list.each do |list| %>
<tr>
<td>
<%= link_to list.site.site_name, trial_trials_path(trial_id: list.trial_id) %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
</tr>
<% end %>
</table>
Setting up the association in your model does write the getter method for you. The attr_accessibles sets up that method.
Add :site to your Trial's attr_accessible