looping and display model's associated data - ruby-on-rails

Am looping through a set of records and would like to display the model's associated data at the same time.
However when trying to display the associated data, am getting an error message
Can anyone tell me What's wrong with the following code?
<% #subs.each do |submission| %>
<%= submission.SUB_OID %>
<%= submission.SUB_ASSAY_TYPE %>
<%= submission.author.AUT_NAME %> -- am getting the error because of this line
<% end %>
Model:
Sub has_one Author
Author belongs_to Sub
If i remove this line <%= submission.author.AUT_NAME %>, the list of submissions are displayed properly, however when i include the 3rd line, i get the error 'Undefines method for AUT_NAME'.
I don't understand where the error is found.
Am a newbie and would be grateful for any suggestion provided

Either your author has no field/method named AUT_NAME or your relationship is wrong. Can you check if submission.author is nil and make sure AUT_NAME exists?

Finally found the solution. I included the following if condition --
<% if submission.author %>
<%= submission.author.AUT_NAME %>
<% end %>
The reason why author name couldn't be displayed was because not all submissions have an associated entry in the authors table.

Related

Access object in the join table

I have a #minisets model and a #miniatures model. They both have_many of each other through the #contents model.
As well as the foreign keys, the #contents model also has a quantity column.
From my #minisets show view I can show the associated #miniatures with the following:
<% #miniset.miniatures.each do |miniature| %>
<%= link_to miniature.name, miniature %>
<% end %>
I want to be able to show the quantity entered for those miniatures but can't work out how to call information from the join table rather than the table it is joining.
Something like <%= miniature.content.quantity %> except that won't work. I assume the joining model must be in play for it to be supplying the joined info but how do I interact with it itself in that instance?
Figured it out.
I need to be working with the join object in the instance variable rather than the joined object.
Find the #contents which belong to this #miniset and then get the #miniature info from there. Makes much more sense.
<% #miniset.contents.where(miniset_id: #miniset).each do |content| %>
<%= link_to content.miniature.name, content.miniature %>
x<%= content.quantity %>
<% end %>
Found some very complicated answers to similar questions but this is dead simple. Hope it helps someone.

Why am I getting an undefined method error in rails after I check if method exists?

On my user profile page in my rails app, I am recieving the error:
undefined method `title' for nil:NilClass
I know it is because of the following 3 lines of code:
<% if #user.profile.title %>
<%= #user.profile.title %>
<% end %>
I don't understand why. Since i use the if statement, shouldn't it first check whether title exists, then if it exists display it, and if it does not exist, it should not display it. What is wrong and how do I fix it? Thanks.
Since title is nil you can't test against it like you did.
Try:
<% if #user.profile.try(:title) #user.profile.title %>
You can do:
<% if #user.profile.present? %>
<%= #user.profile.title %>
<% end %>
Or with try:
<%= #user.profile.try(:name) %>
With a default value if no profile associated:
<%= #user.profile.try(:name) || 'No profile for this user' %>
Because it is not correctly. If you want to get access to some parameter from user you have to use direct access. But if you want to use profile that belongs to User model look at this
link

Rails - Error rendering 'category' model values in the index view

Unable to render category name in the index view, when passing
<%= post.category.name %>
to the
<% #posts.each do |post| %>
Error:
undefined method `name' for nil:NilClass
However, when passing
<%= post.category %>
I get
#<Category:0x007ff5c2c20b68>
Within individual Show actions
<%= #post.category.id %>
works perfectly. What can be the problem?
Thanks
I think that for atleast 1 post, the category is nil
You can avoid the error by making this change
<%= post.category.name if post.category %>
or
<%= post.category.try :name %>
Look at the SELECT commands that are being invoked in each case, you can see it in your terminal where you run rails s
I suspect that for some reason, in the index controller, the categories information is not being retrieved together with the posts
If you don't know what is wrong with the SELECT commands, post both cased here together with the controller and model

Confused about nil rails view

I'm simply trying to ensure a value exists in a certain block and to display it's contents if it does and if not display "Nothing for this yet." I've researched other SO posts but my implementation must be off.
<% #profiles.each do |profile| %>
<%= profile.current_club %>
<% if profile.listings %>
<%= video_thumb_embed(profile.listings.last.video).html_safe %>
<% end %>
<% end %>
Video is an attribute of the listings model.
I'm trying to display all user profiles on an index page and to display the latest video they have added on this index page.
It errors out if a user hasn't added a listing so I added what I thought would be a simple check in the each block but it errors out with the below
undefined method `video' for nil:NilClass
What am I not understanding here? With the above I expect a profile would show on the index page with a video if it had one and if it didn't it wouldn't display anything.
You want to check and make sure that profile.listings is not blank or empty. If you call the last method on an empty array, it returns nil.
<% if profile.listings %> will return true even if you have an empty array.
<% if !profile.listings.blank? %> or <% unless profile.listings.blank? %> should do the trick.

displaying one too many items rails

I am building a simple app and in many views I am displaying all of the objects associated with a certain model (many-to-one relationship). For example, I have a house model and an Item model where House has many Items. On the Show view for house I have the following code:
<% #house.items.each do |item| %>
<% if item.needed == true%>
<p>
<%= item.description %>
</p>
<% end %>
<% end %>
and this displays all the items along with one blank item. If I delete all the items, leaving an empty array there is still one empty item remaining. I can hack this using the code:
<% #house.items[0..-2].each do |item| %>
<% if item.needed == true%>
<p>
<%= item.description %>
</p>
<% end %>
<% end %>
This is probably a really simple question, but I would like to avoid using the latter code, and would like to understand why this is happening. Thanks.
The issue you are seeing is data related. This is to say, you need to figure out what is being returned by #house.items. Perhaps you have an item that has needed == true and a blank description? To trouble shoot this verify what is being returned by the house object in question by opening up the rails console, loading the house object in question and checking what is returned by house.items.
When using #house.items.new to set up a new Item object, it will alter the #house.items array, even though the new item is not yet persisted to the database. Example:
items = #house.items
items.length
# => 3
item = #house.items.new
items.length
# => 4
You could either add a check inside your loop for something like if item.persisted? or unless item.new_record?. Or, you could build the new item this way instead, which won't include it in #house.items until it's actually saved to the database:
item = Item.new(house_id: #house.id, needed: true)

Resources