HStore is great feature in PostgreSQL. It allows us to use “mini-noSQL” db in out pg table. Nice!
Rails4 will have this baked-in, but now we have to use great gem activerecord-postgres-hstore.
The only problem is, that there’s no accessor methods. Let’s assume that our model (Product) need price and weight. We will have to write something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
A lot of repetition. Definetly not DRY. We could use some metaprogramming and create this:
1 2 3 4 5 6 7 8 9 10 11 |
|
Nice, a lot cleaner. But maybe extract this so we could use it anywhere?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Now we can do this:
1 2 3 4 |
|
1 2 3 4 5 6 7 |
|