On Tuesday, November 8, 2016 at 7:30:07 AM UTC-8, David Lazar wrote:
>
> Hi,
>
> I have a JSONB column in my model. in that column I store a hash. An ID
> points to a value. So a basic example of my Hash is...
>
> '{ "111' => 1, '2222' => 2, '321' => 33 }'
>
> The messy part for me is getting at the values... by searching for the
> key. I am not sure I am liking my approach, so am looking for something
> clean...
>
> My JSONB Column is :number_sold, so I start with a simple assignment:
>
> *jb = Sequel.pg_jsonb_op(:number_sold)*
>
> Now I want to do a query on my Model for the key I am interested in, say
> '2222', and I want that value, 2.
>
> If I run the following query it works where an item.product_id will be
> 2222.
>
>
> *MyModel.select(jb.get_text(item.product_id.to_s)).first!.values[:"?column?"]*
>
> Sequel returns an object with @values set to :"?column?" => "2"
>
> So this is now... ugly as heck in my books... is there a more elegant way
> to store a key:value pair in PG and use Sequel to view/update the hash...
> it seems to work fine... but I am not comfortable with this
> .first!.values[;"?column?"] business.
>
Just alias the column:
*MyModel.select(jb.get_text(item.product_id.to_s).as(:foo)).first!.values[:foo]*
*However, it may be better to just use #get:*
*MyModel.get(jb.get_text(item.product_id.to_s))*
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.