Get specific item from object - ruby-on-rails

I'm attempting to return the specific hospital name associated to a patient but keep getting errors.
Models:
Hospital
has_many :patients
Patients
belong_to :hospital
When rendering my page I call the controller:
def list_patients
#patients = Patient.all
end
In my view I print out each patient and their information:
<% #patients.each do |patient| %>
<table>
<tr>
<td><%= patient.first_name + "," + patient.last_name %></td>
<td><%= patient.ssn %></td>
<td><%= patient.dob %></td>
<td><%= patient.hospital.name%></td>
</tr>
</table>
The above returns an "undefined method for name". If I remove name I can see that a object (<Hospital:0x007fa1d9530138>)
is returned, but I'm unable to then access the specific attributes within the object.
I can return the specific hospital ID, if I do something like:
patient.hospital_id
but am then stuck on how to get to the hospital name.

Is your code equal to the pasted one?
If so, is belongs_to and not belong_to
If you also can't do Hospital.first.patients in the console, make sure you have a hospital_id in you patient model

Related

Rails counting rows with has_many relation

I have:
class Constituency < ActiveRecord::Base
has_many :votes
end
class Vote <ActiveRecord::Base
belongs_to :constituency
end
Now, what I want to do is to create a table, that will show me Name of constituency, number of voters (which is easy, as it is part of the table Constituency) and number of votes that were given in this constituency.
The beginning looks like this:
<% #constituencies.each do |constituency| %>
<tr>
<td><%= constituency.name %></td>
<td><%= constituency.voters %></td>
So now comes my question: how can I count all rows in Votes, but with division based on constituency?
constituency.votes.count should give you the correct count for each different constituency. Hope this helps.
<% #constituencies.each do |constituency| %>
<tr>
<td><%= constituency.name %></td>
<td><%= constituency.voters %></td>
<td><%= constituency.votes.count %></td>

Rails view and pulling data within a loop

<% #blog.blog_comment_types.each do |blog_comment_type| %>
<tr>
<td><%= blog_comment_type.comment_type_id %></td>
<td>Comment name goes here</td>
</tr>
<% end %>
I want to be able to output the comment name based off the comment_type_id.
I an looping through the blog_comment_type table, I want to use the "comment_type_id" column so I can pull from the following table: comment_type which has the name field I want to output. The comment_type table has an id which is the referenced comment_type_id being looped through.
Is there a best practice in Rails to do so within a view?
Tbl: comment_type
fields:
id
name
tbl: blog_comment_type
fields:
id
comment_type_id (this is the matching id in the comment_type table).
Thanks!
In Rails and beyond, the best practice is not to do this in a view. Instead, if you setup your Blog object so it knows about the comment types associated with it, then the problem becomes pretty simple:
class Blog
has_many :blog_comment_types
....
end
then in your view:
<% #blog.blog_comment_types.each do |blog_comment_type| %>
<tr>
<td><%= blog_comment_type.id %></td>
<td><%= blog_comment_type.name %></td>
</tr>
<% end %>

Show name instead of ID in has_many view table

I have tried a couple other similar posts but am still getting an error.
In the Posts model I have a category_id field. I have the following models:
#Posts model
belongs_to :categories
#Category model
has_many :posts
In the Posts index controller I have:
#categories = #posts.Category.find(:all, :order => 'categoryname')
In the View I have:
<% #posts.each do |post| %>
<tr>
<td><%= post.category_id %></td>
<td><%= #categories.categoryname %></td>
<td><%= link_to 'View', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
</tr>
<% end %>
In the 2nd column I am trying to show the category name ("categoryname") from the Category table instead of the category_id from the posts table. I am getting an error:
undefined method `Category' for #ActiveRecord::Relation:0x3e1a9b0>
I have also tried:
<td><%= post.categories.categoryname %></td>
But get the same error.
As well as:
<td><%= post.category.categoryname %></td>
Any suggestions would be greatly appreciated.
In your model
belongs_to :category
In your view
<td><%= post.category.categoryname %></td>
You can get rid of the #categories = line in your controller
Also, categoryname is probably not the best attribute name for your Category model. Why not just name it name. post.category.name seems a lot better than post.category.categoryname, don't you think?
Okay, a couple things
belongs_to :categories
belongs_to is a singular relationship. You should be putting
belongs_to :category
In this case you need category_id in the posts table. You would get the category by
#post.category.categoryname
Unless a post can have many categories, in which case you'd want
#Post
has_and_belongs_to_many :categories
#Category
has_and_belongs_to_many :posts
In this case you need a join table called categories_posts with two fields category_id and post_id and you would get it by calling
#post.categories.each do |cat|
cat.categoryname
end
There are some other problems with you code, like
#categories = #posts.Category.find(:all, :order => 'categoryname')
Category is a model, not your named relationship, which is probably why you are getting the exception in your application.
You will get the following error
undefined method `categoryname' for nil:NilClass on the line: <%= post.category.categoryname %>
if there are records in your table without a category specified.
In other words, make sure all of your records have a category associated with them
i Noticed that #category in your controller starts with a small letter c, but in your view, it starts with a Capital letter !
In your case, if there is a chance that you can have an empty category_id in posts table (as you mentioned in your comment), you can add
if post.category_id?
so your cell will look like this:
<td><%= post.category.categoryname if post.category_id? %></td>
If post.category_id is null it will just show an empty cell.

RoR how to find a record backwards

I have a 3 table - users, employees, contacts.
The employees and contacts have a foreign key pointing the the associated user.
Employee belongs_to :user
Contact belongs_to :user
User has many employees, contacts
But, I would like to list the employee name or contact name in the user index view.
Do I need to use a find_by_sql statement?
Thanks,
Dave
You have the belongs_to associations set up already, so you can add the has_one association in your User class for employee and contact, then you can do #user.employee.name or #user.contact.name.
You definitely don't need find_by_sql for this.
In your index view,
<table>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.employees.map{ |empl| empl.name }.join(", ") %></td>
<td><%= user.contacts.map{ |contact| contact.name }.join(", ") %></td>
</tr>
<% end %>
</table>
The only down side is it will do a query for all the user's employees and all the user's contacts in the loop for user. If the page loads reasonably fast, code it as above, because that's clearer code.

How can I display data for the current view from a different table

I have two tables:
Chords - |id|chord|name|rating|artist_id|
Artists - |id|artist|
An Artist has many Chords, and thus a Chord belongs to an Artist.
And in the index page for "chords" I want to display chord, name, and rating from Chords table and the artist from the artists table
This is the code for the Chord's index.html.erb:
<table border="1">
<% #chords.each do |chord| %>
<tr>
<td><%= chord.artist.artist %></td>
<td><%= link_to chord.name, chord %></td>
<td><%= chord.rating %></td>
<td><%= chord.created_at %></td>
</tr>
<% end %>
</table>
The error message is:
undefined method `artist' for nil:NilClass
Actually, at first it worked, but when I started to create the "new.html.erb" page and the create and new actions, it stopped working, that's why this is so confusing to me!
Since chort.artist can be null, you should change chord.artist.artist to chord.artist.try(:artist) which is shorthand for
if chord.artist.nil?
nil
else
chord.artist.artist
end
You could use
<td><%= chord.artist.artist unless chord.artist.artist.nil? %></td>
In the view is no the right place to do it but what you need to do is find the artist with the ID given in the CHORDS like this
#currentArtist = Artist.find(:all, :conditions => {:protectora => Chord.artist_id})
Then when you find it by the ID given in the CHORD and save it in to a variable, you can access it like any other variable so:
#currentArtist.artist
Hope it helps.

Resources