I'm not even sure if this title is the best description.
I'm building some basic filtering capabilities via a form for my events. Events have a category from a select drop down.
Now when you want to filter, you can select via checkboxes the categories you want to display.
I'm a bit stumped how to do that. Is it possible to do it all in one query? Or do you separate it into 1 for each category?
My old query was this current_user.friends.events(:event, :rel).where("rel.admin = {admin_p} AND event.detail = {detail_p}").params(admin_p: true, detail_p: true).pluck(:event)
In this case, I would need something like event.category = category1, category2, cateogry3 . Obviously this isn't how it's written. Ways to achieve this?
In cypher, IN lets you match results within an array.
MATCH (u:User)-[r1:INVITED_TO]->(e:Event) WHERE e.uuid IN [1, 2, 3, 4, 5] RETURN e
That would match events with any of the uuid properties in that array. In the Ruby gem, this is handled automatically for you in QueryProxy if you use an array in a where method.
current_user.events.where(category: [1, 2, 3, 4, 5])
In your form, ensure that each checkbox's value corresponds with its ID. Put those IDs into an array and search as demonstrated above.
Related
I'm stuck with this problem. I;ve built an API in Rails and a client in Angular. I've got a model called Todo which has a property called order. When I create a new Todo, I automatically assign the newly created todo a value order like so:
#todo = Todo.create(todo_params)
#todo.order = Todo.count + 1
And to display all todos:
#todos = Todo.where(...).order(:order.desc)
In my client I'm using Sortable for the UI manipulation and there's a method called onUpdate which gives me the new indexes for the array.
The problem I'm having is that the indexes are considerably different than the value I've in the order property. When the aforementioned function is called I get the item and two more values: newIndex and oldIndex, but the problem is that this doesn't give me the index of the other todos, so how can I reorganise this on the database?
I'd appreciate any help as I'm a bit at loss here.
PS: Please note this is Rails API only, so no views.
EDIT
Output wanted:
Let's imagine this: I have three items in the database: {name: A, order: 1}, {name: B, order: 2}, {name: C, order: 3}. In the front-end, while using ng-repeat, each item will have an $index. Let's say for argument sake that A will have $index value of 0, B 1, C 2. At the moment, when I use the Angular library Sortable, when I manually sort the list, I get the values: oldIndex and newIndex which corresponds to the position of the item I've moved within the array, but it does not shows the position of all items within array, so I don't know how to use the properties these values to update the object in the database.
For anyone that might be interested: At the end I've used the following solution:
Every time Sortable updated an item, it also sends an array with all items on it in the correct order. I then looped through the array and updated each item with the new order. Not sure this is the most efficient way to do it but it resolved the problem for now.
This is what my query looks like:
Connection.where("invited_user_id = :user_a_id AND inviter_user_id = :user_b_id", user_a_id: self.inviter.try(:id), user_b_id: self.invited.try(:id)).exists?
What I would like to do is basically check invited_user_id against multiple options, e.g. something like invited_user_id = [:user1, :user2]. But that doesn't work.
How can I check to see if the value in the column invited_user_id is in either of the variables :user1, :user2 or any other of multiple variables.
Is that possible without using an explicit OR approach that's less DRY?
Never put values directly in a query, that's not safe. Parameterize your query using the ? operator.
invited_user_ids = [:user1, :user2]
Connection.where("invited_user_id IN (?)", invited_user_ids)
The Hash form of where accepts array of values, so:
Connection.where(invited_user_id: [1, 2, 3, 4, 5])
That will find all records where invited_user_id is any of the values listed in the array.
I have an array of ids. Now I want to get all the documents corresponding to the ids inside that array from a collection.
Is there any command by which I can achieve this?
I don't want to run a loop over that array and query for every element of the array.
Assume the array is
id = [1,2,3,4]
The collection is Scores, which has the field id among other fields.
I'm looking for something like Scores.find(..)
In ActiveRecord, the following query works as expected
Scores.find([1, 2, 3, 4])
Depending on the MongoDB adapter you use, it may work as well. According to this documentation, the same syntax is also supported in Mongoid.
Otherwise, you can generally use
Scores.where(id: [1, 2, 3, 4])
that will return a collection of records matching the given IDs.
in a scenario i want to retrieve the records with different values so i used distinct for that,
Book.where(["user_id = ?",#user_id]).select('distinct title_id')
`this, only retrives the records like this [#<Book title_id: 30>, #<Book title_id: 31> ]`
but i want to fetch the id of Book as well along with title_id
so, please advise me how to work on this
thanks
use grouping:
Book.where(:user_id => #user.id).grouped('title_id')
problem is that if you do grouping you can't have different book ids, they are all grouped into single row. You can use GROUP_CONCAT to workaround that:
Book...select('books.*, GROUP_CONCAT(id) as ids')
that way you'll have book ids attribute for every group
noob question here. I have a has_many :through relationship between items and tags. Finding all items associated with a given tag is simple enough:
things=Tag.find(1).items
But what if I want to find all items associated with more than one given tag? I was thinking something like:
things=Tag.find(1).items
Tag.find(2).things # wrong, but you get the idea
You can use the array union operator.
things = Tag.find(1).items | Tag.find(2).items
That will create an object for every item for both tags, which could be way too much depending on what you're trying to do. If you want something a little more scalable, you can do the lookup on the join table.
things = ItemTags.find_by_sql("
SELECT item_id, COUNT(tag_id) AS tag_count
FROM item_tags
WHERE tag_id IN (1, 2)
GROUP_BY item_id
HAVING tag_count = 2;
").map(&:item)
Just wrote that in browser, so it could be completely wrong. Also, there is probably a way to do that with activerecord finders that would be nicer that a find_by_sql.
things = Things.where(:tag => [1, 2])
things = Things.where('tag in :tags', [1, 2])
etc...