how to get previous and next records for a specific record in rethinkdb - rethinkdb

I am trying to get the surrounding records based on a given record id.
For example I have this records:
[{id: 1, name: 'a'},{id: 2, name: 'b'},{id: 3, name: 'c'},{id: 4, name: 'd'},{id: 5, name: 'e'},{id: 6, name: 'f'}]
Let's say I got the record id 3 and I wanted to get it's previous and next records, in this example I want to get record 2 and 4.
I am using rethinkdb-dash in my application.
Thanks

Related

There is error while uploading csv file in play console. How to avoid it?

invalid row(s) in the CSV file: 1, 2, 3, 4, 5, 6, 7, 8, 9 ..etc
Your format is incorrect should make like this:
As in picture you only can add email address in one column with multiple rows

Ruby - Split the string and store it in a Array based on the first word

I am pretty much new to Ruby and I am looking at the best way to do the below.
I have a string like this "Account 1 - Name, Account 1 - Age, Account 2 - Name, Account 2 - Age"
I am looking for an output something like this
[[Account 1, Name], [Account 1, Age], [Account 2, Name], [Account 2, Age]]
Certainly I don't want to post the ways I tried as it looks silly and ugly. I am looking for a single liner if possible. Many thanks and appreciate all your help!
Looks pretty straightforward. You need to split once based on ,, and then again based on -. The first split already stores your data into an array for you so you don't need to do anything else.
string = "Account 1 - Name, Account 1 - Age, Account 2 - Name, Account 2 - Age"
array = string.split(', ')
array = array.map { |acc| acc.split(' - ') }
# [[Account 1, Name], [Account 1, Age], [Account 2, Name], [Account 2, Age]]

Efficient way to get difference of two streams in RethinkDB

I am running some performance benchmarks on RethinkDB (related to a specific use-case). In my simulation, there are 2 tables: contact and event. A contact has many events. The event table has 2 indices: contact_id and compound index on [campaign_id, node_id, event_type]. The contact table has about 500k contacts and about 1.75 million docs in event table.
The query I am struggling with is to find all the contacts who have sent event_type but not open event_type. Following is the query I got to work:
r.table("events").
get_all([1, 5, 'sent'], {index: 'campaign'})['contact_id'].distinct
.set_difference
(r.table("events").get_all([1, 5, 'open'], {index: 'campaign'})['contact_id'].distinct)
.count.run(conn)
But this query uses set difference, not stream difference. I have also tried using difference operator:
r.table("events").
get_all([1, 5, 'sent'], {index: 'campaign'})['contact_id'] .difference
(r.table("events").get_all([1, 5, 'open'], {index: 'campaign'})['contact_id'])
.count.run(conn)
This query never finishes and the weird thing is even after aborting the query I see (in RethinkDB dashboard) that the reads dont stop.
Whats the most efficient way of doing these kind of queries?
Follow up: find all the male contacts who have sent event_type but not open event_type. What I have now is:
r.table("contacts").get_all(r.args(
r.table("events").get_all([1, 5, 'sent'], {index: 'campaign'})['contact_id'].distinct
.set_difference
(r.table("events").get_all([1, 5, 'open'], {index: 'campaign'})['contact_id'].distinct)))
.filter({gender: 1}).count.run(conn)
One way to make this efficient is to denormalize your data. Instead of having separate contact and event tables, just have the contact table and make each contact have an array of events. Then you can write:
r.table('contacts').indexCreate('sent_but_not_open', function(row) {
return row('events').contains('sent').and(
row('events').contains('open').not());
});
That will work well if the number of events per contact is smallish. If you have thousands or millions of events per contact it will break down though.
RethinkDB doesn't offer a way to diff two streams lazily on the server. The best you could do is to change your compound index to be on [campaign_id, node_id, event_type, contact_id] instead, replace your get_all([1, 5, 'sent'], {index: 'campaign'}) with .between([1, 5, 'sent', r.minval], [1, 5, 'sent', r.maxval], {index: 'campaign'})and then put.distinct({index: 'campaign'})['contact_id']on the end. That will give you a stream of distinct contact IDs rather than an array, and these contact IDs will be sorted. You can then do the same for theopen` events, and diff the two ordered streams in the client by doing a mergesort-like thing.

Dataset Graph with separate hashes per table

The Sequel documentation states that
Dataset graphing changes the dataset to yield hashes where keys are table
name symbols and values are hashes representing the columns related to
that table. https://github.com/jeremyevans/sequel/blob/master/lib/sequel/dataset/graph.rb#L5
I'd really like to use this behaviour, so when I do something like;
db[:referrals].graph(:users, :id => :user_id).first
I would get;
{referrals: {id: 1, name: 'Joe', user_id: 10}, users: {id: 10, name: 'Dave'}}
However, what is returned is more like;
{id: 1, name: 'Joe', user_id: 10, users_name: 'Dave'}
I can't see any obvious way to change this in the docs. Am I missing something blindingly obvious (wouldn't be the first time).
The docs need to be updated (I'll take care of that shortly). You now need to use the graph_each extension to get the splitting.

Rails and Padrino ActiveRecord ignores GROUP_CONCAT in .select

ActiveRecord in Rails 3 or Padrino is ignoring GROUP_CONCAT inside a .select.
I'm trying to figure out why AcitveRecord is ignoring this query:
Dvd.includes(:dvd_director, :dvd_producer).
select("
GROUP_CONCAT(DISTINCT dvd_director.director SEPARATOR ', ') AS director
, GROUP_CONCAT(DISTINCT dvd_producer.producer SEPARATOR ', ') AS producer
...
.where("id = 4")
The query gets executed but all the GROUP_CONCATs get ignored and the info is not in the result. This is a simpliefied version of this question which hasn't been answered.
I read somewhere that you should use .cacluate for this, but that gives me an error.
What gives?
RAILS 4
I had a similar problem with my query:
#project
.fields_numbers_projects
.select("*, GROUP_CONCAT(fields_numbers_projects.dates_project_id) AS dpid")
.group(:group_id)
.each do |fpn|
And the result of ActiveRecord was:
<#FieldsNumbersProject
id: 1, group_id: 1, project_id: 1, dates_project_id: 1, fields_project_id: 1, employee_id: nil>
<#FieldsNumbersProject
id: 1, group_id: 2, project_id: 1, dates_project_id: 1, fields_project_id: 1, employee_id: nil>
It had no sign of "dpid" or just GROUP_CONCAT(dates_project_id) in the ActiveRecord result whereas you could use it as fpn.dpid.
I got fooled for ten minutes by the ActiveRecord result without trying to print.
I think I later read that this wasn't supported because AR has no way of knowing which DB technology you're using, so some features are unavailable, and apparently GROUP_CONCAT is not available to every SQL tech out there.

Resources