If the product name is nil then use another name? - ruby-on-rails

I am currently developing a cart which will take the id of the CD or a DVD, it then gets the name of the item.
If I use them individually it works fine as I have made the relevant changes in the other files.
The code currently is:
<% if line_item==#current_item %>
<tr id='current_item'>
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.name %></td>
<td><%= line_item.dvd.name %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price, :unit=>'&pound') %></td>
</tr>
If I have a <td><%= line_item.cd.name %></td> line and only add CDs it works fine, and vice-versa with this line <td><%= line_item.dvd.name %></td>.
What I want is it to do a test to see if cd.name is nil and if it is use the dvd.name, but this is not working.

This should work
line_item.cd.try(:name) || line_item.dvd.try(:name)
This code will first try to get CD name and fallback to DVD if either cd or cd.name is nil.

In your Item model, try adding this method:
def cd_or_dvd
line_item.dvd || line_item.cd
end
Than in the view:
<td><%= line_item.dvd.name %></td>

Related

Using .blank? with postgres

Hi chaps and chappettes.
<tbody>
<% #orders_outstanding.limit(5).each do |order| %>
<% if order.completed_at.blank? && order.due_date.present? %>
<tr>
<td><%= order.order_number %></td>
<td><%= order.customer %></td>
<td><%= order.printer %></td>
<td><%= order.quantity %></td>
<td><%= order.due_date %></td>
</tr>
<% end %>
<% end %>
</tbody>
I'm using this little bit of code to display my next five orders due to ship. It's showing up in my development environment preview (puma/sqlite) but not on heroku (postgres). Is there any reason heroku doesn't like that formatting?
Thanks
I would put the conditions in the controller to make sure you have 5 that match your conditions:
#orders_outstanding = Order.where(completed_at: nil).where.not(due_date: nil).order("due_date")

Display Ancestry name instead of id

My applications currently uses the Ancestry gem to create a navigation tree.
How do I display the name of a page which is the parent of another in the index view?
i.e. currently I do...
<% #pages.each do |page| %>
<tr>
<td><%= link_to page.name, edit_page_path(page) %></td>
<td><%= page.ancestry %></td>
</tr>
<% end %>
I want the page.ancestry to be the parent's name, not the ID.
<td><%= page.parent.name %></td>
isn't it working?

Compute multiple distinct averages

I'm fairly new to rails, and am still getting used to putting together methods. I'm currently trying to create a method that averages distinct data from multiple columns. I'd like to do it all in one method so that I can display the information easily in an html table.
Currently I have this in my model:
def averagedonate
scores.group(:donatedate).average('donateamount')
scores.group(:donatedate).average('rating')
end
I'd like to be able to use them in a table like this:
<% #averagedonate.each do |donatedate, donateamount, rating| %>
<tr>
<td><%= donatedate %></td>
<td><%= donateamount %></td>
<td><%= rating %></td>
</tr>
How do I change my averagedonate method to do this? Thanks in advance!
I haven't tested, but something to this effect should work
def averagedonate
scores.select("
AVG(donateamount) as avg_donateamount,
AVG(rating) as avg_rating,
donatedate
")
.group(:donatedate)
end
Then use it like this
<% #averagedonate.each do |item| %>
<tr>
<td><%= item.donatedate %></td>
<td><%= item.avg_donateamount %></td>
<td><%= item.avg_rating %></td>
</tr>
<% end %>

Rails 3 each do ignore nil values

I am building an html table that should include name, rating1, rating2, and rating3. rating 1-3 come from different models than name.
resources :names do
resource :rat1,:rat2,:rat3
end
Inside of my html table I'd like to include the ratings from within each of these tables but I would like to automatically skip over or ignore tables that are nil. This is because :names may only have a :rat1 and not a :rat2 or :rat3. My view should look something like this.
<table>
<thead>Name</thead>
<thead>Rating 1</thead>
<thead>Rating 2</thead>
<thead>Rating 3</thead>
<% #names.each do |name| %>
<tr>
<td><%= name.nametext %></td>
<td><%= name.rat1.rating %></td>
<td><%= name.rat2.rating %></td>
<td><%= name.rat3.rating %></td>
</tr>
<% end %>
</table>
Except that if name.rat1 is nil it will either a.) replace the value with N/A OR b.) it will leave this field blank and move on to the next.
What is the cleanest way to do this?
::UPDATE::
So my issue is that the name.rat1 is nil and the name.rat1.rating is an undefined method of a nil class so both of these options will throw the same undefined method of a nil class error regardless of the || or helper method. At least thats what my current tests are showing. Any other options? or different workarounds? I'd like to avoid having to put a validation loop like this for every rat1-3
<% unless name.rat1.nil? %>
<%= name.rat1.rating %>
<% end %>
There has to be a simpler way.
I would probably create a helper method in names_helper.rb
def show_rating(rating)
if rating.present?
rating
else
"default value"
end
end
Then use it in the view:
<%= show_rating name.rat1.rating %>
OFFTOPIC Your table structure is wrong. It should have <thead><tr><th>Name</th><th>Rating1</th>..so on..</tr></thead>
So, in your case you can use the condition while rendering the rating values as:
<table>
<thead>
<tr>
<th>Name</th>
<th>Rating 1</th>
<th>Rating 2</th>
<th>Rating 3</th>
</tr>
</thead>
<tbody>
<% #names.each do |name| %>
<tr>
<td><%= name.nametext %></td>
<td><%= name.rat1.rating || 'N/A' %></td>
<td><%= name.rat2.rating || 'N/A' %></td>
<td><%= name.rat3.rating || 'N/A' %></td>
</tr>
<% end %>
</tbody>
</table>

How do I output name instead of ID?

Rails newbie here! I have a has_many_and_belongs_to_many relationship between items and builds.
In the code provided below, the ID of the items are being displayed instead of the name of the item with that ID. How do I display the name of the item?
<% current_user.builds.each do |build| %>
<tr>
<td><%= build.hero.name%></td>
<td><%= build.user.email%></td>
<td><%= build.name %></td>
<td><%= build.starting_items %></td>
<td><%= build.early_items %></td>
<td><%= build.core_items %></td>
<td><%= build.situational_items %></td>
</tr>
<% end %>
If you mapped association (has_one) in model with Hero and User, then probably you can use following code:
build.hero.field_name # Where field_name is column name from Hero Model
Please let me know if this will work for you or not.

Resources