Here have three tables with association and relationship as follows
Categor table is the main table
Product table belongs to categor table where its product_id references categor(id)
Sale table belongs to Product table where its sale_id references product(id)
Here am am trying to get records based on their refrences where:
just consider this three tables as Categor as the main table, product as sub table and sales as sub table under product
1.) categor table will loop and display all the records in it.
2.) product table should loop and display its record based on categor_id where it matches id of the categor table.
3) sale table should loop and display its record based on product_id where it matches id of the product.
just consider this three tables as Post, comment and reply
def index
##categors = Categor.where(:id => '1')
#categors = Categor.all
#prods = Product.where(:categor_id => '1')
#sals = Sale.where(:product_id => '1')
end
At this point, I can successfully display all the records but can not get it be aligned properly.
Categor is the main table.
Products info should be under Categor.
sales info should be under products.
Just view it as post, comment and reply respectively.
<p id="notice"><%= notice %></p>
<div>
<% #categors.each do |categor| %>
Categorid: <%= categor.id %>
Category Name: <%= categor.cat_name %>
Ctaegory Label<%= categor.car_label %>
</div>
<% end %><br>
<% #prods.each do |pr| %>
Product id: <%= pr.id %>
Product Name: <%= pr.prod_name %>
<% end %>
<br>
<% #sals.each do |s| %>
sales id: <%= s.id %>
sales Name: <%= s.sales_name %>
<% end %>
<br>
<br>
I'm not sure I understand what you are trying to accomplish here, but I'm going to assume you are trying to display all categories, and all products in each category and all sales for each product.
There are several different ways that you could structure this, like for example tree views or nested tables, etc, and you should take in consideration how useable the page will be once you have a lot of categories/products/sales, but for the sake of simplicity let's structure them as nested lists:
<ul>
<% #categors.each do |categor| %>
<li><%= categor.cat_name %>
<ul>
<% categor.products.each do |pr| %>
<li><%= pr.prod_name %>
<ul>
<% pr.sales.each do |s| %>
<li><%= s.sales_name %></li>
<% end %>
</ul>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
Now you didn't show us how your models are setup so i don't know if the relationships between them are properly set but I'm assuming yes.
Related
I'm working on a project that needs to do some basic statistic reporting. For example, the model Post has a foreign key field category_id referring to a record in the Category model, and boolean field published which has a default value of false (along with title, body, author - but they're not relevant to this question).
I want to set up and nested grouping to iterate through the Post model and group the Post records by their Category, and then within each category, further group by the state of the published field, to give a count for each grouping, to render an outcome similar to below:
Categories
Tutorial
Published: 14 posts
Draft: 3 posts
Q & A
Published: 14 posts
Draft: 3 posts
Letter
Published: 14 posts
Draft: 3 posts
Below is the non-functional code I've started with:
<% #posts.group(:category).each do |category| %>
<% category.label %>
<% category.each.group(:published).count.each do |published_status, count| %>
<%= published_status %>: <%= pluralize(count, "post") %>
<% end %>
<% end %>
Any feedback or suggestions on how to modify the code above would be very much appreciated. Thanks
Please try below query it is not tested but i think it should work
NOT TESTED
categories = Category.left_outer_joins(:posts)
.select("
Categories.label,
(SELECT COUNT(posts.id) from posts where posts.published=true) as published_count,
(SELECT COUNT(posts.id) from posts where posts.published=false) as draft_count
")
.group('categories.id')
In View
<% categories.each do |category| %>
<%= category.label %>
<%= category.published_count %>
<%= category.draft_count %>
<% end %>
I am working on a RoR WebApp. I'm trying to group results on the search page based on their taxonomy. What I want to do is to show a header for a category and list all results under that category. Something like:
CAT 1
products
CAT2
products
CAT3
.
.
I am trying using the following code:
<% if products.any? %> #products is the list of search results
<%= render :partial=> 'product_listing_feature', :locals => {:scope => scope, :scope_type => scope_type} %>
<div id="ql_product"></div>
<div class="product_rows">
<%taxons.each do |taxon|%> # taxons contains the list of unique categories in products
<div class = "product_row">
<h1><%=taxon%></h1>
<% taxonProducts = Array.new %>
<% products.each do |product| %>
<%#ptaxon = product.get_taxonomy%>
<%if #ptaxon == taxon%>
<% taxonProducts.push(product) %>
<% end %>
<% end %>
<div class ="featured_product_list">
<ul class = "featured_products">
<div class = "page">
<%= render :partial=> 'product_listing', :locals=>{:collection=> taxonProducts} %>
</div>
</ul>
</div>
</div>
<% end %>
</div>
<% end %>
Surprisingly it starts the 2nd category from a new row, but the following categories appeared jumbled up, something like
CAT1
products
CAT2
products CAT3
products
picture would give a better idea.
I am really surprised why it works only for one iteration. Could someone please help me fix this.
Thanks a lot
Way, way too much logic for a view. Just use group_by in your controller, which will give you a mapping of names to arrays of products:
products = Product.includes(:taxon).group_by { |p| p.taxon.name }
I am trying to group a set of products (obtained typing a query) based on taxonomy(on of the attributes of a product)
My desired output is
Taxonomy 1
prod1 prod2 prod3 prod4
prod 5 ...
Taxonomy 2
pod6 prod7
Taxonomy 3
prod8 prod9..
I am using the following code in the view:
<% taxonomies.each do |taxonomy|%> #"taxomonies" is a set of unique taxonomies for retrieved products
<h1><%= taxonomy%></h1>
<ul>
<% collection.each_with_index do |product,i| %> #"collection" is the list of products retrieved
<li>
<%#ptaxon = product.get_taxonomy%>
<%if #ptaxon == taxonomy%>
<%code for listing product%>
<%end%>
</li>
<%end%>
</ul>
<%end%>
This groups the products based on taxonomies but the format is not what I desire. Could someone please point out my mistake.
EDIT: also tried using < br > , but doesn't help!
This is the output I'm getting. I want the taxonomies earrings, bracelets and necklaces to start from a new line.
Thanks
If you have your associations setup correctly, you can do it like this:
<% taxonomies.each do |taxonomy| %>
<%= taxonomy.name %>
<% taxonomy.products.each do |product| %>
<%= product.name %>
<% end %>
<% end %>
Models should be something like:
class Taxonomy
has_many :products
end
class Product
belongs_to :taxonomy
end
I am new in RoR.
The problem is, I created fully functional product categorization with Ancesrty. But now I want to be able to retrieve products that is under these subcategories.
This is my categories show controller
#category = Category.find(params[:id])
Here is categories#show view.
<b>Name of the category:</b>
<%= #category.name %>
<div class="product"
</div>
</p>
<% unless #category.children.empty? %>
<ul id="sub-menu">
<% #category.children.each do |sub1| %>
<%= link_to (sub1.name), sub1 %>
<%end%>
<%end%>
It all works fine. but now I want to add in view categories/show function that shows all products that is under that category.
I added such code.
In category/show controller
#cat_id = #category.id
#product = Product.where("category_id = ?",#cat_id)
In the categories show view I added
<td><%= #product.name %></td>
Then clicking on some subcategory where should appear few products, there just shows up Product
To check if the code is right I put in the console. There it works fine and retrieve products related to this category.
I dont understand why then code not working in webserver when I launch application ?
Could it be because of some erorr in Associations ?
Thanks !
in your controller, a more readable way is to use the plural form to indicate that you are expecting more than 1 object
#products = Product.where("category_id = ?", #cat_id)
Then in the view, just loop through these products
<% #products.each do |product| %>
<%= product.name %>
<% end %>
#product = Product.where("category_id = ?",#cat_id)
will return an array if there are any products. So you will need to loop through the array.
<% #product.each do |product| %>
<%= product.name %>
<% end %>
I accept both of the answers, But I want to suggest to use Active Record Association for this type of problems. This makes your solution easier.
If you want to fetch only one product, you can use the find_by_ helper method of the model:
#product = Product.find_by_category_id(#cat_id)
With this it will fetch the first matching product which has category_id equal to #cat_id.
If you want to fetch all the products which belong to a category, you need to fetch all the products as others suggested:
#products = Product.where(:category_id => #cat_id)
And then in the view:
<% #products.each do |product| %>
<%= product.name %>
<% end -%>
I have a model that have a column for name and a column for categories. There are a large amount of names that I would like to list by category but I haven't figured out how to do it.
Currently in the view I have
<% for car in #cars %>
<%= car.name %>
<% end %>
which just presents this huge list of that is way too unwieldy. I'm using #car = Car.find(:all) in the controller to get the selection.
=> If there a way I can create some form of dynamic table where it groups all the car records by category and makes a new column for each category instance, with a listing of all the associated cars?
=> I'm also afraid that that might be too many columns so after 5 or so columns can I start a new row?
=> Should I do all this in the controller or the view?
Can you specify some sample values for the category column?
Depending on what you have stored you may be able to do this:
Car.find(:all).group_by(&:category)
Which will put the cars into an ordered hash indexed by the different category values.
<% #car_categories.sort.each do |category, cars| %>
<h3><%= category %></h3>
<% for cart in cars %>
<p><%= interest.name %></p>
<% end %>
<% end %>