Rails sort list of results - ruby-on-rails

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}")

Related

Bigcommerce Ruby

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.

Ruby on Rails: where returning nil

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.

How to get a unique list of associated results?

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

Is there a way to order the results of a query with a non standard order?

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

How to find items where ID is not 1,2,3

I'm trying to create a list of items but in that list of items I need to make sure 3 of them are not in the list.
How do I achieve this in Rails?
I'm trying to get this to work:
not_droppable = [126,127,128]
#items = Item.where('id not in (?)', not_droppable)
#items = Item.all.delete_if {|x| not_droppable.include?(x.id)}
Item.all will always return an array. Use Array#delete_if to knock out Item elements from the collection whose ids are included in the not_droppable array.

Resources