rurabe's comments

rurabe | 3 years ago | on: Understanding the N + 1 queries problem

Big fan of raw sql, but practically speaking (as it relates to developing with rails) CTEs can be rewritten as subqueries, the advantage being that they are linear instead of nested in SQL.

With AR queries you can do the same and make it linear in ruby (and then the computer doesn't really care if your sql is nested)

    last_three_posts = Post.limit(3).order(created_at: :desc)
    @posts = Comment.where(post_id: last_three_posts)

rurabe | 3 years ago | on: Understanding the N + 1 queries problem

The problem here is that you are loading all the votes as AR instances which is fine at small scale, but as your app gets larger, loading and instantiating thousands of Vote instances just to then break them down into an integer will start to drag on your controller.

If you can count in the database itself it's a big win. Although no doubt your solution is cleaner code.

rurabe | 3 years ago | on: Understanding the N + 1 queries problem

One neat trick that I think is relatively lesser known is that you can select arbitrary sql expressions in ActiveRecord and those values are made available on the instances.

(Also I think the above sql needs to be tweaked since you need the votes count grouped by comment not by post)

A one to many relationship in pure SQL is an awkward fit with a Rails app as it requires serializing (at least) the many as json. Then there's this weird conceptual gotcha where one resource is an AR instance and another is a pure hash.

I'd probably make a scope and association to help out here:

    class Comment
      scope :with_vote_count, ->{ joins(:votes).select('comments.*').select('count(votes.*) as vote_count') }
    end

    class Post
      has_many :comments
      has_many :comments_with_vote_counts, ->{ with_vote_counts }, class_name: 'Comment'
    end

    # in controller
    @posts = Post.includes(:comments_with_vote_counts).limit(3).order(:created_at: :desc)

    # in view/serializer, posts and comments are both AR instances
    @posts.each do |post|
      post.comments.each do |comment|
        comment.vote_count # => Integer
      end
    end
This should give you 2 queries, one to load the posts, then one to load the comments and vote counts for the relevant posts. Controller stays nice and slim and the complexity is delegated to sql via the join scope, without any other dependencies.

* edited for HN code block syntax

rurabe | 4 years ago | on: Defensive tactics from the modern history of urban warfare

What about incorporating Ukraine, Belarus, Georgia, and Russia into NATO?

Ukraine gets the Donbas back in return for international recognition of Crimea as Russian territory.

Fanciful, I know. And questionable whether Article 8 would hold up. But advantages:

1. End to the conflict 2. Security guarantees for all of Europe 3. Repurposing of NATO from anti Russia alliance to anti China alliance, ie pivot to Asia

rurabe | 4 years ago | on: Defensive tactics from the modern history of urban warfare

Honestly, I fear the resistance being too effective.

Russia is on the offensive when viewing this conflict in isolation. Zoom out to geopolitics and it is very much on the defensive.

They are locked in this conflict and if they cannot achieve their goals they will escalate. They also own the most nuclear weapons of any country on earth.

This is what the Art of War says when it says not to put enemies in a corner.

This is also a very realpolitik take on this. It goes without saying that all of this is a humanitarian disaster.

rurabe | 4 years ago | on: Defensive tactics from the modern history of urban warfare

The military endgame sadly is somewhere between a destroyed Ukraine in perpetual conflict and regime change, depending on the efficacy of Ukrainian resistance.

Sorry to say, autocrats do not withdraw from a conflict like this regardless of attrition. Their power is their legitimacy and defeat is a threat to both their rule and probably their life. I mean look at how much flak Biden took withdrawing from Afghanistan despite being able to say that it was a horrible idea and someone else's fault.

This is a little different from Afghanistan and Iraq though, Russia's security concerns are valid and probably ameliorated by Ukraine as a failed state (as opposed to a NATO aligned state) so conquering and pacifying the country is not necessary.

The only real humanitarian solution is a diplomatic solution. I wonder if written guarantees that Ukraine and Georgia will never join NATO would be enough now honestly.

rurabe | 4 years ago | on: U.S. Supreme Court revives LinkedIn bid to shield personal data

Yeah each user gets the choice right now as a consequence of LinkedIn's product design.

As far as I know, there's no reason it couldn't be "Log in to see this profile".

I believe the injunction bars LinkedIn from blocking hiQ specifically from accessing the publicly available information (which again is public by LinkedIn's choice). They made a point to draw a distinction between the public stuff and stuff behind a login/password.

rurabe | 4 years ago | on: U.S. Supreme Court revives LinkedIn bid to shield personal data

This is a pretty bad headline. I don't know that i would characterize this as revived.

The same 9th circuit who held last year that LinkedIn could not block hiQ from scraping public data, just got asked to reconsider the same case, except now there is additional precedent that SCOTUS says if you had permission to access the computer then it's not a violation of the CFAA (even if you are a shady corrupt cop).

Hard to see this turning out any other way than the 9th circuit reaffirming their decision (or even strengthening it) and then it's up to LinkedIn to try SCOTUS again

rurabe | 4 years ago | on: Replit used legal threats to kill my open-source project

yeah i mean same argument about why they shot harambe because some kid wanted to climb the fence into the gorilla exhibit.

i actually agree that corporations have too much power versus individuals in society. but it's a bit much to jump into the gorilla exhibit and then write a clickbait post about whether it was unethical.

rurabe | 4 years ago | on: Replit used legal threats to kill my open-source project

dude literally emailed the ceo and was like yeah i used this component, but what else could i have used, and yeah i used this other thing i learned about from working there but its popular! and i used yet another thing but you guys werent using it when i worked there.

im not saying any of this is illegal. just weird to copy your previous employer's tech stack, open source it, and try to play the victim and clickbait HN.

rurabe | 4 years ago | on: Replit used legal threats to kill my open-source project

i mean you're right, he doesn't have to. but i'm pointing out that he's not being personally persecuted for no reason. doing literally anything else than open sourcing his employer's tech stack makes the whole concern about needing protection pretty unnecessary.

ianal but absent some nda seems like it's probably legal for him to do it.

it just seems like kind of a dick move. i could mentor new engineers, wait until someone told me about a really cool idea, then steal it from them and build it myself. there's no law against it, it's just is kind of a dick move and seems kind of wrong.

as noted above, ceo is acting like a dick as well. but i think the way this dude is trying to play the victim through clickbaiting HN is a bit much. just my opinion.

rurabe | 4 years ago | on: Replit used legal threats to kill my open-source project

with regard to ethics, two plausibly valid options:

either you believe that people should act ethically even if that means forgoing actions that they are legally allowed to make. if you believe that, isn't it wrong to build a free, open source competitor using all the knowledge you just got from working inside a company?

or:

you believe that business is the law of the jungle, and everyone is free to do whatever they can get away with legally. in which case why is it a problem for amasad to get lawyers involved?

it seems this dude's victimization relies on holding amasad to a higher standard of ethics than he holds himself. maybe he should because he has more money and power. or maybe not?

page 1