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!
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 :)
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)
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.
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.
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.
Thank you it was very helpful..
given your use case, why not using Postgres array feature instead of JSON?
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!
what would be the performance if let says you have a couple thousand products and each having few tags of off couple hundred tags?
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 :)
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)
Nice, thanks for sharing!
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.
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.
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.
@@rapid-ruby you're right. I left that comment before I watched you gave that caveat haha.