Rails Active Record Query Finding Item Out Of Scope - rails-activerecord

I'm not sure I fully understand active record querying, but I am running into a very peculiar issue where my active record conditionals seem to be pulling in items outside my current scope. Here is an example of what I am seeing:
In Rails 2.3 console
>> Transaction.single_card.find(:all).map(&:id)
=> [0, 1, 2, 3, 4, 5]
>> Transaction.single_card.find(:all, :conditions => "cards.number = '1234'").map(&:id)
=> [9]
<this line added because the [9] was being cut in half by scroll bar>
How is this happening? Why, when I add extra conditions to my query, do I pull a record that should not be there at all? From my understanding, the extra conditional should check Transactions 0..5 (the transactions with a single card) and see if the card number is 1234. But the query pulls Transaction 9, which has 2 cards associated with it, which is why it did not appear in the initial query. What is going on?
Extra note: The single_card named scope :includes the cards reference

Related

Consecutive Event Sequence Matching in Clickhouse

I am trying to do some funnel analysis using Clickhouse. I am aware of sequenceMatch/windowFunnel functions but they allow events in between sequences. I am trying to show how many users navigated to a certain a certain path with different querystring params consecutively.
Given the following array [url, eventsequence]
['/someurl/page?a=1', 1]
['/someurl/page?a=2', 2]
['/someurl/page?a=3', 4]
['/someurl/page?a=4', 5]
['/someurl/page?a=4', 6]
I would like to evaluate that the above sequence of events saw the user navigate directly from page to page 3 seperate times, events 1->2, 4->5 and 5-6.
Worked this out - you can pass in a sequence to sequenceCount and use the pattern which says make sure the events have no gaps
(?1)(?t<=1)(?2)
sequenceCount('(?1)(?t<=1)(?2)')(sequence,
ilike(page, '%a%'),
ilike(page, '%a%')) as sequences

Is there a drawback in using rxjs for readonly collection manipulation

I need to do a Min and Max operation on a array getting from server side.
I am new to rxjs extensions but those library is actually mean to observe changes on a collection, but in my case its just a ONE time calculation on a collection which is no further changed then until I do a server side refresh of the data.
I just want to use the right tool for the right job, thus I ask is it correct to use rxjs here or is that shooting with bombs on flys?
Or should I rather use a library like https://github.com/ENikS/LINQ
to get the Min/Max value of a collection?
There is a LINQ implementation IxJS that is developed and maintained by the same team that is developing RxJS. This might be the right tool for you.
However, you could go with RxJS as well. When using Rx.Observable.from([1, 2, ...]) the execution is synchronous on subscription.
I would use IxJS however:
// An array of values.. (just creating some random ones here)
const values = [2, 4, 23, 1, 0, 34, 56, 2, 3, 45, 98, 6, 3];
// Create an enumerable from the array
const valEnum = Ix.Enumerable.fromArray(values);
const min = valEnum.min();
const max = valEnum.max();
Working example on jsfiddle.
https://github.com/ENikS/LINQ uses all the latest language features and theoretically much faster than IxJS. Last edit on IxJS is 3 years old. (ECMA-262/6.0/) introduced few very important advancements and speed improvements.
It also has better compliance with standard LINQ API and can operate on any collection implementing iterables, including strings, maps, typed arrays, and etc. IxJS can only query array types.

activerecord update wrong number of arguments

Sorry if this is a simple problem that has been explained before. I've done some research about my problem. I'm completely new to ruby and active record and I find the examples other have had with the wrong number of arguments too complicated for me to follow. So here is my simple one.
I'm trying to do a simple update using activerecord to a db. All I'm trying to do add a value to the title attribute that I left as nil when I created it in the first place.
vertigo is the variable I assigned using the .find method.
I'm typing in vertigo.update(title: 'Vertigo')
But I'm getting an error message saying
wrong number of arguments (1 for 2).
Here is more of session. I'm using Sinatra-tux >> vertigo = Movie.all
D, [2015-04-20T11:11:38.890714 #3741] DEBUG -- : Movie Load (0.4ms) SELECT "movies".* FROM "movies"
=> #]>
vertigo.update title: "Vertigo"
ArgumentError: wrong number of arguments (1 for 2)
/home/michael/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.0.4/lib/active_record/relation.rb:330:in update'
(ripl):4:in'
find can return an array. It's entirely possible that your vertigo variable is actually an array.
Try to run
vertigo[0].update(title: 'Vertigo')
assuming you only want to change the first one.

Modeling data in Redis

I am building a system that keeps track of many counters in real time in Redis. Each counter is basically the impression, conversion details for ad keywords shown on a specific url.
ie. if 10 keywords are shown on a specific url, I need to update a count for each of those keywords for both impressions and conversions. And on each impression of a url, possibly a different set of 10 keywords can be shown.
ie. the basic data model I need is something like
> url=>
k1 =>
impression => 2
conversion => 1
k2 =><br>
impression => 100
conversion => 8
.
.
k100 (max around 100)</li>
I understand Redis doesnt have nested hashes so I cant store a 2 level hash as I have shown above.
What is the best way to solve this problem?
I thought of combining k1-impression and k1 conversion and making it one single field
ie like
url =>
k1-impression => 100
k1-conversion => 3
.<br>
. so on</li>
But the problem is the lengths of 'k1', 'k2' etc is significant ( 120-150 bytes) and I dont want to replicate that data, if possible, to save on memory.
How would I go about solving this problem?
Any help will be appreciated.
If your keywords are of significant enough length that you're worried about it, you should normalize them. Make a hash of keyword -> id, and a hash of id -> keyword, for encoding and decoding them. Then you can have per-url hashes of the form url => {kw_id:impressions => 1123, kw_id:conversions => 28}. This will also serve you well when you start needing to make indexes of the key words, which you will as soon as you get a requirement to show the top 10 best performing key words across all urls, for example.

recursive nested loops

Example Scenario: Note, this can be as deep or as shallow depending on the website.
Spider scans the first page for links. it stores it as array1.
spider enters the first link, it's now on second page. it sees links, and stores it as array2.
spider enters the first link on the second page, its now on third page.
it sees links upon, and stores it as array 3.
Please note that this is generic scenario. I want to highlight the need to do many loops within loops.
rootArray[array1,array2,array3....]
how can i do a recursive nested loops ? array2 is the children of each VALUE of array1 (we assume the structure is very uniform, each VALUE of array 1 has similiar links in array2). Array 3 is the children of each Value of array2. and so on.
module Scratch
def self.recur(arr, depth, &fn)
arr.each do |a|
a.is_a?(Array) ? recur(a, depth+1, &fn) : fn.call(a, depth)
end
end
arr = [[1, 2, 3], 4, 5, [6, 7, [8, 9]]]
recur(arr, 0) { |x,d| puts "#{d}: #{x}" }
end
You'll want to store these results in a tree, not a collection of arrays. Page1 would have child nodes for each link. Each of those has child nodes for its links, etc. An alternate approach would be to just store all of the links in one array, recursing through the site to find the links in question. Do you really need them in a structure analogous to that of the site?
You'll also want to check for duplicate links when adding any new link to the list/tree/whatever that you've already got. Otherwise, loops like page_1 -> page_2 -> page_1... will break your app.
What's your real goal here? Page crawlers aren't exactly new technology.
It all depends on what you are trying to do.
If you are harvesting links then a hash or set will work well. An array can be used too but can lead to some gotchas.
If you need to show the structure of the site you'll want a tree or arrays of arrays along with some way of flagging which urls you've visited.
In any case you need to avoid redundant links to keep from getting into a loop. It's also real common to put some sort of limitation on how deep you'll descend and whether you'll remember and/or follow links outside of the site.
Gweg, I just answered this on your other post.
How do I create nested FOR loops with varying depths, for a varying number of arrays?

Resources