from Hacker News

How to build a search engine with Ruby on Rails

by alokrai on 9/16/21, 10:58 PM with 63 comments

  • by gscho on 9/17/21, 1:50 AM

    This is a bit off topic but I wanted to say I’m really glad there are still people out there hacking on some ruby + Postgres projects and writing about it. I feel that ruby is an excellent language and dread the demise of it.
  • by gouda-gouda on 9/17/21, 6:01 AM

    Ruby is alive and well. We don’t use Rails specifically (Hanami + ROM.rb + DRY.rb), but absolutely love Ruby as a language and the ecosystem surrounding it. It’s productive, and as powerful as you need it to be.

    When it comes to building feature-rich web applications quickly and sanely, Ruby is still hard to beat IMO.

  • by pqdbr on 9/17/21, 2:07 PM

    Great article. By the way, ActiveRecord's #merge is golden, and I'm under the impression it's not as mainstream as it should be.

    I use it extensively to avoid duplicating scope code.

    For instance:

    class Listing

       scope :active, -> { where("expired_at > ?", Date.current).where.not(suspended: true) }
    
    end

    class User

      has_many :listings
    
    end

    So instead of doing this (which is terrible):

    user.joins(:listings).distinct.where("listings.expired_at > ? AND listings.suspended != FALSE", Date.current)

    You can simply:

    user.joins(:listings).distinct.merge(Listing.active)

    Rails docs are amazing, but #merge doesn't get enough love. Maybe I'll issue a pull request to improve it with some examples like this and the ones from the article.

  • by Toutouxc on 9/17/21, 8:25 AM

    I love the section about getting rid of Active Record only to discover that the handwritten SQL equivalent is unmaintainable and slower.

    Also if you're on the fat-models team and you have lots of behaviors and business logic on your models, it's often easier to just go with Active Record, because the second you wander off the beaten path (e.g. handwritten joins) you start dealing with weird franken-models that contain attributes from multiple tables, but not their behaviors (methods, callbacks).

  • by gsinkin on 9/17/21, 12:42 PM

    Great post at the perfect time. I am currently wrestling with some gnarly, expensive queries in postgres and this gives me some great leads to try out.

    Also, I appreciate the Rails love.

    I know this is not a Who Is Hiring post, but if you are into Rails/Postgres and in the market (between UTC-4 and UTC-8 timezones), feel free to send me a note: gabe at instrumentl.com. We are doing some "biggish" data work helping nonprofits find grants and other fundraising opportunities.

  • by elesbao on 9/17/21, 12:27 PM

    Good post, well written, taught me a couple of things I didn't knew and uses Ruby in a clean way. I don't think it meant to be uber scalable but last time I had to build search in ruby I had to use Ferret (a great lib) and it was not this straightforward.
  • by moffkalast on 9/17/21, 9:47 AM

    Everyone asks how to build a search engine with Ruby on Rails, but nobody asks why build a search engine with Ruby on Rails...