Ask A Question

Notifications

You’re not receiving notifications from this thread.

EAV Rails way?

Anand Padia asked in Rails

Hi Team,

I was trying to explore possibility of using EAV (Entity-Attribute-Value) pattern in our application. We are a ecommerce solution and would need Product variants to be captured, I did some quick research and found 2 gems claiming to do it right.

https://github.com/iostat/eav_hashes
https://github.com/kostyantyn/hydra_attribute

Has anyone used them.. Also do we have any reference videos available on them.

Reply

Ah ha! So EAV is what that's called. I had always known about this but never know what to call it. :)

It looks like the eav_hashes gem might be worth a shot since it's been updated relatively recently. There might be a fork that's been created more recently that has some other fixes or new features with someone maintaining it.

One alternative that I'd suggest looking into is actually the json or jsonb columns in Postgres. They are similar to the serialized text columns, but they're fully queryable. It's a little different but may be worth checking out as a solution as well.

I need to read up on EAV a little bit more myself.

Reply

Is there any benefit to using the EAV pattern versus a jsonb column? I've been using jsonb in a few of my more recent projects and it's great to work with, but you can feel a noticeable difference in queries if it has to iterate over a substantial amount of records. So I've found it best for storing all the variable style data that you may not really need to query against, which sounds like what you're needing to accomplish.

About a quarter of the way down on this page => http://nandovieira.com/using-postgresql-and-jsonb-with-ruby-on-rails they dive into the migration setup. One of the interesting things I thought was that they're using GIN indexes - I wonder if there is any such functionality for the setup you're describing Anand?

One thing to note, jsonb support in postgresql is "relatively" new, so if you're working with an older rails app you may have issues. I believe they introduced it in Postresql 9.4.

Reply

I'm not up to speed with much of the json(b) column indexing. This talks a bit about it briefly: https://blog.codeship.com/unleash-the-power-of-storing-json-in-postgres/

Reply

Thanks for the response on this, I am using Rails 4.2.6 with Ruby 2.3.3. Jsonb seems to be interesting and i would try that out. I will share the results.. Thanks Jacob & Chris for your inputs.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.