Adding tags to content using Postgres’ JSONB in Rails

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ม.ค. 2025

ความคิดเห็น • 11

  • @ricardomordaunt1101
    @ricardomordaunt1101 ปีที่แล้ว +1

    Thank you it was very helpful..

  • @stpaquet
    @stpaquet ปีที่แล้ว +1

    given your use case, why not using Postgres array feature instead of JSON?

    • @rapid-ruby
      @rapid-ruby  ปีที่แล้ว +1

      I tend to find I use json columns a lot and don’t find any drawbacks when using it compared to array columns, plus it’s less operators to learn when I know I’ll use json columns already. Hope that helps!

  • @h0lyd1v3r0
    @h0lyd1v3r0 ปีที่แล้ว +1

    what would be the performance if let says you have a couple thousand products and each having few tags of off couple hundred tags?

    • @rapid-ruby
      @rapid-ruby  ปีที่แล้ว +1

      This should work fine, you can also add gin/gist indexes to these columns to increase performance, but it shouldn’t be much different from a standard column :)

  • @thevelopr3949
    @thevelopr3949 6 หลายเดือนก่อน +1

    Awesome, thank you! To make it case insensitive I did this:
    array_of_tags = PG::TextEncoder::Array.new.encode(tags)
    where("lower(tags::text)::jsonb ?| :tags", tags: array_of_tags.downcase)

    • @rapid-ruby
      @rapid-ruby  6 หลายเดือนก่อน

      Nice, thanks for sharing!

  • @aarona3144
    @aarona3144 ปีที่แล้ว +1

    Re: the Post.find_by_tags, is it possible to do something like: Post.find_by(tags: ["Rails","Hotwire"]) ? If not, I think there might be a gem that can do this.

    • @aarona3144
      @aarona3144 ปีที่แล้ว +2

      One more thing, use Array.wrap inside your find_by_tags method so you can send it one tag or an array... it will get converted to an array regardless of what you send it.

    • @rapid-ruby
      @rapid-ruby  ปีที่แล้ว +1

      Nice, that’s a great tip!
      To your first point, yeah you’d need a custom gem to do this, for how simple this is, I’d personally prefer not to pull in a dependency for it, but ymmv.

    • @aarona3144
      @aarona3144 ปีที่แล้ว +1

      ​@@rapid-ruby you're right. I left that comment before I watched you gave that caveat haha.