Bigcommerce Ruby - ruby-on-rails

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.

Related

Find records related to other records in controller (ruby on rails)

I'm grabbing a list of users and storing in #users.
Now I need to find properties related to only this list of users I have queried.
if params[:company].present?
#users = User.where(parent_id: params[:company]).or(User.where(id: params[:company]))
##properties = #properties.where(user_id: params[:company])
end
I would basically like to include #users inside #properties.where()
I need to get each property that has a user_id present in my #users array
edit:
I just did the following which gives me the result, however, I'm sure there's a much better way of doing this via activerecord:
ids = []
#users.each do |user|
ids.push(user.id)
end
#properties = #properties.where(user_id: ids)
#properties.where(user_id: #users.ids)
That should work. It'll take the id of user ids and perform a filter using the IN clause.
Perhaps adding your models and their relationships we can think about something better.

Ruby on Rails controller order with strings

So I'm trying to organize one of my views so that the articles of my website are listed by the name of the title. For example some of my articles are named "article pt.1, history pt.2, society pt.1, etc". I do have them sorted correctly using this line of code in the Articles controller
def index
#articles = Article.order(:user_id)
end
It does work using the user id, but if I wanted to add another category and have them in alphabetical order, I would need to update the user id of each article which is not practical as they number a little over 200 in the database. What I would like to is somehow take a partial of the article title and sort each one like I am with the user_id field. This way I can sort each one using the string partial such as "article" from "article pt.1"
Thank you for reading and have a wonderful day!
Why not just sort by the title? Assuming you have a column in your articles table called title:
For alphabetical order
def index
#articles = Article.order(:title)
end
For reverse alphabetical order
def index
#articles = Article.order(title: :desc)
end
If you really want to just sort by a substring of the title. You'll have to add a new column to the articles table (called slug in this example):
rails g migration AddSlugToArticles slug:text
rails db:migrate
Then you'll have to update the slug field of every record
Article.all.each do |article|
new_slug = #your code to generate substring here
article.update(slug: my_string
end
then order by slug:
def index
#articles = Article.order(:slug)
end
First of all, it is not very clear about the output that you want, but based on assumption and the description mentioned in the post, it seems like you want to sort the string field not based on whole value but based on substring.
It is better to use the order to get the strings alphabetically sorted.
#articles = Article.order(:title)
And it will also serve the purpose as it will first match the first alphabet of each string and also handle null values at the same time.
Why write a custom logic if the purpose is fulfilled by an already defined method.
I was able to do it using the sort function instead of order using the :title field on the articles controller.
def index
#articles = Article.sort(:title)
end

RoR: How to sort an array with the help of scopes

I have an array #products. Each element of the array is a hash, containing a few fields (but not all) from Product table and the corresponding values.
I have a scope descend_by_popularity in Product which allows me to sort the products based on popularity field. I'd like to sort the array #products using this scope.
What I tried:
#product_group = Array.new
#products.each do |product|
#product_group.push(Product.find(product['id']))
end
#product_group1 = #product_group.descend_by_popularity
But this gives me error:
undefined method `descend_by_popularity' for #<Array:0xb2497200>
I also want to change the sorted Product list back to the format of #products array.
Thanks
Scopes only make sense within the ActiveRecord context for requests to the database (since it is used to change the SQL query). What you did is throwing a lot of products into an array. This array then knows nothing about the scope anymore. You would have to use the scope when you create the #products object. (and it does not seem to make a lot of sense to move the result of a query into an array)
So something like
#products = Product.descend_by_popularity.where(some more stuff)
should work for you. After that you should have the records in the order defined by the scope and can then either use them directly or still push them into an array if that's what you want to do.
With the updated info from the comments it looks like maybe the best way to go would be to first collect only the Product ids from the solr response into an array and then run that as search together with your scope:
#product_group = #products.map{|product| product.id}
#result = Product.where(id: #product_group).descend_by_popularity
this should technically work, peformance is a different question. I would consider aggregating this data into the Solr document, if it doesn't change too often.
Now assuming you are only interested in the order of products as such, you could do something like this to get #products into this order:
#result.map{|r| #products.find{|p| p[:id] == r.id}
though this may slow down things a bit.
Try this: find_by_id as params
#product_group = Array.new
#products.each do |product|
#product_group.push(Product.find(params['id']))
end
and return the array of #product_group
#product_group1 = #product_group.descend_by_popularity

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

Resources