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
Related
I have two models and I want to display them in one table.
Model 1:
class Name < ActiveRecord::Base
has_one :employer, :foreign_key => 'application_id'
end
Model 2:
class Employer < ActiveRecord::Base
belongs_to :name, :foreign_key => 'application_id'
end
Controller:
def summary
#name = Name.all
end
I have this in my view:
<% #name.each do |e| %>
<tr>
<td ><%= e.application_id %></td>
<td ><%= e.Name_of_employee%></td>
<td ><%= e.Employer_name%></td>
</tr>
<% end %>
The name only have one employer.
I am getting "undefined method `Employer' for #
"
TYIA!
as the association is defined,
has_one :employer, :foreign_key => 'application-id'
notice the case employer, so it has to be small case, and not class name
<td ><%= e.employer.name %></td> # name or some other attribute you wish to display
Also, including the association first will help eliminate n+1
#name = Name.includes(:employer)
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 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
How to sort this by count of events?
I tried diffrences ways but it still doesn't work:
<tr>
<% User.all.each do |user| %>
<td> <%= user.name %></td>
<td> <%= user.events.count %></br></td>
<% end %>
</tr>
And this is a model:
class User < ActiveRecord::Base
has_many :events
Thanks for answers.
You can achieve it through counter_cache
class Event < ActiveRecord::Base
belongs_to :user, dependent: :destroy,counter_cache: true
end
Add a column events_count to your users table like this
add_column :users, :events_count, :integer, :default => 0
and use it in the view like this
<tr>
<% User.all.each do |user| %>
<td> <%= user.name %></td>
<td> <%= user.events_count %></br></td> #here
<% end %>
</tr>
I would like to suggest to look into this Railscasts,but it is old.
Look in to this plugin And here is an example how it is done.
Hope it helps.
I have these three models.
Student.
Evaluation.
Grade (student_id, evaluatioin_id, value)
I want to make a form so that a User can set the grade for each student of a evaluation...
I want to keep this is as clean as possible (and restful)...
Im open to any suggestions on how to get this done.
Please Help.
See these
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
I suppose you have the following associations:
Evaluation:
class Evaluation < ActiveRecord:Base
has_many :grades
accepts_nested_attributes_for :grades
end
Student:
class Student < ActiveRecord::Base
has_many :grades
end
Grade:
class Grade < ActiveRecord::Base
belongs_to :student
blongs_to :evaluation
end
Regarding your comment I think you want to use the nested_form_for gem made by Ryan Bates. It helps you add/delete dynamically nested attributes in your form, very efficiently. You can use it like this:
<% nested_form_for #evaluation do |f| %>
<% f.fields_for :grades do |gf| %>
<%= gf.label 'Username of the student' %>
<%= gf.collection_select(:student, :student_id, Student.All, :id, :username) %> <br/>
<%= gf.label :value %>
<%= gf.text_field :value %> <br/>
<%= gf.link_to_remove 'delete' %> <br/>
<% end %>
<%= f.link_to_add 'Add grade', :grades %> <br/>
<%= f.submit %>
<% end %>
Tell me if it works/fits your needs. I have been using this gem a lot these previous days.
What I ended up doing was..
In Grade Model
scope :for_student,lambda{|student, evaluation| where("student_id"=>student.id).where("evaluation_id"=>evaluation.id)}
In EvaluationsController
def assign_grades])
#evaluation = Evaluation.find(params[:id])
#students = Student.all
#students.each do |s|
grade = Grade.for_student(s,#evaluation)
if !grade.exists?
#evaluation.grades.build(:value=> 0,:student_id=>s.id)
end
end
end
def save_grades
#evaluation = Evaluation.find(params[:id])
#evaluation.update_attributes(params[:evaluation])
redirect_to [#evaluation.batch_subject, #evaluation]
end
In View
<table class="medium">
<thead>
<tr>
<th>name</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<%=f.fields_for :grades do |g|%>
<tr>
<td><%=g.label :value,g.object.student.full_name%> <%= g.hidden_field :student_id%></td>
<td><%=g.text_field :value%></td>
</tr>
<%end%>
</tbody>
</table>