I want to search for a Product and get all the categories from all the products from the search result to create a filter interface like github (The "Languages" menu in the sidebar).
#products = Product.product_search(params[:q])
#categories = ?
I want to list all categories from the products in the #products array.
Any idea whats the best way to do this?
I think this should do the trick.
#categories = #products.map(&:category).compact.uniq
Related
I have list of categories
#categories = Category.all
There are about ten categories.
But I need to change order of them being displayed.
I need category with id = n to be shown first.
This categories already created and filled with items.
So what is the most elegant way to change #categories and put category with id=n first?
#categories = Category.order("id != #{n}")
Attempting to sort an entry by an associated field, unfortunately when paginating with Will Paginate the sorting is wrong.
#categories = Category.includes(:posts)
.page(params[:page])
.order("posts.post_at DESC")
I have tried many variations, but unfortunately will paginate does not seem to respect the sort order of categories. Without the pagination the categories are sorted correctly.
How can I get Will Paginate to respect the order?
The end goal is Categories sorted by posts.post_at and paginated while maintaining that order.
Thanks.
Try
#categories = Category.includes(:posts).order("posts.post_at DESC").paginate(:page => params[:page], :per_page => 10)
Sorry can't comment due to low rep, but what are you trying to sort by and what do you want listed?
If you want categories in a certain order with the associated posts, is that by alphabetical?
#categories = Category.includes(:posts).order('categories.name??').page(params[:page])
Category with most recent posts?
#categories = Category.includes(:posts).order("posts.post_at DESC").page(params[:page])
Or are you wanting the most recent posts with the categories listed along with it?
#posts = Post.includes(:category).order('posts.post_at DESC').page(params[:page])
It's unclear what your end goal is with this query.
I am referencing this: https://github.com/bigcommerce/bigcommerce-api-ruby/blob/master/examples/products/product.rb
Here is the code:
# List products
#products = Bigcommerce::Product.all
puts #products
# Get a product
#product = #products[0]
puts Bigcommerce::Product.find(#product.id)
I understand the #list products, but it appears that #get a product is just pulling the first item in the array #products and displaying it?
I don't understand this: Bigcommerce::Product.find(#product.id)
End goal is to search #products for a specific property value. Like where SKU = some SKU or title = some time or price = some price, etc.
Also, is #products a hash or an array?
So confused. :(
#products looks like it should be a collection of instances of Bigcommerce::Product. Based on that, #product = #products[0] assigns to #product the first of the collection.
I do not understand:
puts Bigcommerce::Product.find(#product.id)
I would think that this would suffice:
puts #product
I understand the #list products, but it appears that #get a product is
just pulling the first item in the array #products and displaying it?
Yes. It seems like just an example.
I don't understand this: Bigcommerce::Product.find(#product.id)
This returns a Product object with id == #product.id.
Also, is #products a hash or an array?
It is an array. See the documentation for ActiveRecord's all method.
EDIT In this API, SKU and Product are different resources (see product-related resources here). To search by SKU, you should do this:
# Get a product sku
puts Bigcommerce::Sku.find(#product.id, #sku.id)
See the example. SKUs contain a reference to their Product, so you can search by SKU and then get the Product you want.
EDIT 2 Bear in mind that Resources in this API are subclasses of Hashie::Trash, not ActiveRecord which would be more usual, so we can't rely on things like find_by.
In my app I'm obtaining a certain category, and I'm filtering the associated items based on their name.
The following code should be pretty clear:
categories = Category.where(:id => params[:category_id]).includes(:items).where("lower(items.name) like ?", "%#{params[:keywords].downcase}%")
However, if the name filter excludes all the items, the categories object returned by where is nil. Is this the expected behaviour? How can I get the category even either items exist or not?
The easiest way might be to just split the query:
#category = Category.find(params[:category_id])
#items = #category.items.where("lower(items.name) like ?", "%#{params[:keywords].downcase}%")
Based on your code it seems like category_id references only 1 category so I've changed it to singular.
You should look into doing an OUTER JOIN against the items table which will give you categories regardless of whether or not their items meet the name filter.
My site allows users to change the order in which categories and what categories are displayed, and it stores that order as an array in the db. so I have:
#categories = Category.where("id IN (?)", current_user.order)
the problem is order is something like [2,4,3,6,1] and then #categories has order [1,2,3,4,6].
I want to order the #categories so it matches the order.
Any help would be appreciated
Store user preferences in a separate table and then JOIN userOrderTable with #categories
Another approach might be to create an empty array and iterate over the Categories:
#categories = []
current_user.order.each do |category_id|
#categories << Category.find(category_id)
end