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.
Related
I have number of restaurants. On the fly i sorted out restaurants and stored to an array. Later I converted this array to rails object. When I did this array sorted order gone. How can I keep array order while converting to an rails object?
Edit:-
#restaurants = [res1,res2,res3,res4,res5]
sorted_array = [res1,res5,res2,res4,res3]
#places =Restaurant.where(:id=> sorted_array.map(:&id))
then i got below order of rails objects
#places = [res1,res2,res3,res4,res5]
I want to order like below when I convert an array to a rails object.
How can I get same order as an array ?
#places = [res1,res5,res2,res4,res3]
This seems to do what you want (res1 ... res5 should be Restaurant objects)
ids = [res1,res5,res2,res4,res3].map(&:id)
#places = Restaurant.where(id: ids).order("FIELD(id, #{ids.join(',')})").all
More info, Maintaining order in MySQL "IN" query
Is that sorted array an array of Restaurant records? Curious because you're
trying to call #id on the map block. Where are res1-res5 defined?
Another thing to note is your & syntax looks off, I think you need to say sorted_array.map(&:id), otherwise it's trying to call #&id on each restaurant record.
If it's a small array length like 5, in this case, you could map from the sorted array:
#places = sorted_array.map { |restaurant| Restaurant.find(restaurant.id) }
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}")
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
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
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