Getting info to insert from a belongs_to relationship - ruby-on-rails

I've got orders, which have many line items, and line items which belong to products. I'm trying to extract the product title, but it's not appearing in the output. I'm getting the line_item info fine. They are all linked in the line_items table, which has both order_id and product_id fields. I'm pretty new to rails, please can someone help me figure out where I'm going wrong?
<% #order.line_items.each do |line_item| %>
<tr>
<% line_item.product do |product| %>
<td><%= product.title %></td>
<% end %>
<td><%= number_to_currency(line_item.price) %></td>
<td><%= line_item.quantity %></td>
<td><%= number_to_currency((line_item.price*line_item.quantity))%></td>
</tr>
<% end %>

Try:
<% #order.line_items.each do |line_item| %>
<tr>
<td><%= line_item.product.title %></td>
<td><%= number_to_currency(line_item.price) %></td>
<td><%= line_item.quantity %></td>
<td><%= number_to_currency((line_item.price*line_item.quantity))%></td>
</tr>
<% end %>

Related

how to write each loop for has_many in rails?

employee has many skills
skill belongs_to employee
Skills table have employee_id(foreign key).
I want to display in view
There are 4 ways to get the collections of users.
#employees = Employee.all.joins(:skills)
#employees = Employee.all.includes(:skills)
#employees = Employee.all.eager_load(:skills)
In your view add below code in HTML/HAML/SLIM file.
<% #employees.each do |employee| %>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.map(&:title).join(", ") %></td>
</tr>
<% end %>`
You could try with something like this.
In controller return all the employees
so it will be like
#employees = Employee.all.includes(:skills)
and in the view you can do
<% #employees.each do |employee| %>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.collect(&:title).join(", ") %></td>
</tr>
<% end %>
Best practice in this case is to write an instance method on your Employee class:
class Employee < ApplicationRecord
has_many :skills
def skills_list
skills.pluck(:title).join(', ')
end
end
And use it like this in your view:
<table>
<tr>
<td><%= #employee.id %></td>
<td><%= #employee.name %></td>
<td><%= #employee.skills_list %></td>
<tr>
</table>
You can use:
<table>
<tr>
<th>Employee </th>
<th>Skills</th>
</tr>
<% #employees.each do |employee|%>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.map(&:name).join(', ') %></td>
</tr>
<% end %>
</table>
This above code will list all skill names of each employee.
You can read a complete guide here

Loop multiple variable to form one table

What would be the best method to loop multiple variables into one table? Each variable could have one or more data points. For example, I wish to create something like this:
The following code example is only placing each item in a row.
<table class="table">
<% (#variable1 + #variable2 + #variable3).each do |data, v2, v3| %>
<tr>
<% if data.is_a?(Variable1) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% elsif data.is_a?(Variable2) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% elsif data.is_a?(Variable3) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% end %>
</tr>
<% end %>
</table>
In this case, since you don't know how many records might be in each array, I would find out the greatest length of the arrays I have.
In your controller for this aciton could do something like:
#max_length = [#variable_1, #variable_2, #variable_3].map(&:size).max
*Note: this assumes those variables are always arrays, even if they only have a length of 1.
Then in your view file you could do this:
<table class="table">
<% #max_length.times do |n| %>
<tr>
<td><%= #variable_1[n].try(:date) %></td>
<td><%= #variable_1[n].try(:name) %></td>
<td><%= #variable_2[n].try(:date) %></td>
<td><%= #variable_2[n].try(:name) %></td>
<td><%= #variable_3[n].try(:date) %></td>
<td><%= #variable_3[n].try(:name) %></td>
</tr>
<% end %>
</table>
This way, for the third row, for example, in columns 1 and 3 in your example there will be no values, but values for the second column, which has a third record, will appear.

show products that belong to orders on admin pannel

I am trying to show the name of each product that belongs to an order on my admin panel but I cant seem to get it.
here are my associations:
OrderItem belongs_to :product
OrderItem belongs_to :order
Product has_many :order_items
Order has_many :order_items
I have tried two different things:
<tbody>
<% #orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<td><%= order.order_items.product.name %></td>
<td><%= order.total %></td>
</tr>
<% end %>
</tbody>
This gets me this error:
undefined method `name' for #<Array:0x0000000535fbb0>
So then I try to loop through like this:
<tbody>
<% #orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<% order.order_items each do |order_item| %>
<% order_item.products each do |product| %>
<td><%= product.name %></td>
<% end %>
<% end %>
<td><%= order.total %></td>
</tr>
<% end %>
</tbody>
And I get this error:
undefined local variable or method `each' for <Class:0x007fce540aa3f0>:0x007fce5417fed8>
Here is my controller code:
def orders
#orders = Order.all
#order_items = OrderItem.all
#products = Product.all
end
I am not sure where I am going wrong any help would be appreciated, thanks.
You can do this in either way you suggest: just keep an eye on your collections vs. items
First Way:
This one puts all your items in a single <td>
<tbody>
<% #orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<td><%= order.order_items.map{ |o| o.product.name }.join(', ') %></td>
<td><%= order.total %></td>
</tr>
<% end %>
</tbody>
Second Way:
Here, each order_item gets its own <td>
<tbody>
<% #orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<% order.order_items each do |order_item| %>
<td><%= order_item.product.name %></td>
<% end %>
<td><%= order.total %></td>
</tr>
<% end %>
</tbody>
You might consider adding some has_many through or delegate relationships in your models in the long term - it will make this kind of association a little tidier.

How to get unique elements through array and group them in ruby on rails

i have issue. This below code is the views code of ruby in rails
<table >
<tr>
<th>Url</th>
<th>Tags</th>
</tr>
<% #array_bookmark = #bookmark.class == Array ? #bookmark : [#bookmark] %>
<% Array(#bookmark).each do |book| %>
<tr>
<td><%= book.url %></td>
<td><%= book.tags %></td>
</tr>
<% end %>
</table>
This yields something like :
Url Tags
www.mrabhiram.tumblr.com abhi
www.mrabhiram.tumblr.com blog
google.com google
google.com blog
But, i want to get it as
Url Tags
www.mrabhiram.tumblr.com abhi,blog
google.com google,blog
Can anyone provide me the solution? It should be generic enough to iterate over the array.
Thank you in advance.
<% Array(#bookmark).group_by {|b| b.url}.each do |url, books| %>
<tr>
<td><%= url %></td>
<td><%= books.map {|b| b.tags}.flatten.uniq.join(" ") %></td>
</tr>
<% end %>
Use group_by statement
upd
<% Array(#bookmark).group_by(&:url).each do |url, books| %>
<tr>
<td><%= url %></td>
<td><%= books.map(&:tags).flatten.join(',') %></td>
</tr>
<% Array(#bookmark).uniq.each do |book| %>
<tr>
<td><%= book.url %></td>
<td><%= book.tags %></td>
</tr>
<% end %>
the above will work.

Trying not to repeat myself with Rails data manipulation

I'm fairly new to rails, an an intermediate programmer in general, but I'll try to be as clear as I can.
I have a database table for Products, which owns a field called category. Category has two possible values, call them Category1 and Category 2. The goal is to retrieve all the Products and display them in HTML organized by Category. I can make this work, but I know there has to be a better way.
My method now is to get my Products like so:
#category1_products = Product.all(conditions: { category: "Category1" })
#category2_products = Product.all(conditions: { category: "Category2" })
and then output the data something like this:
<table>
<tr>
<td>Category1 Name</td>
<% #category1_products.each do |product| %>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<% end %>
</tr>
<tr>
<td>Category2 Name</td>
<% #category2_products.each do |product| %>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<% end %>
</tr>
</table>
I would like to accomplish this using a single instance: get all Products at once, group them by Category, and then output them by looping through one Category at a time (including the Category Names). This seems like a pretty elementary concept to me, but I'm having trouble getting my head around it.
#products_by_category = Product.all.group_by(&:category)
This is a Ruby (read: not ActiveRecord) method to turn an Array into a Hash based on some condition of the objects it contains.
In your view (assuming you want one row per product, and not one per category):
<table>
<% #products_by_category.each do |category,products| %>
<tr>
<th><%= category %></th>
</tr>
<% products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
</tr>
<% end %>
<% end %>
</table>
Rails' group_by is great for this type of problem:
#grouped_products = Product.all.group_by(&:category)
and then in the view, just loop over #grouped_products:
<table>
<tr>
<% #grouped_products.each do |category, products| %>
<td><%= category %></td>
<% products.each do |product| %>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<% end %>
<% end %>
</tr>
</table>

Resources