本文介绍了查找论文中,数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在数据库中存储的标签以数组的字符串

I am currently storing tags in the database as a string of arrays

我想基于阵列上找到的文章

I want to find articles based on the array

Article.find_by(tags: 'xbox')

但是,这带来了一个错误。

But this brings an error.

还有:

Article.where(tags: 'xbox')

这告诉我,我必须插入 {} 找到阵列以

This tells me that I have to insert {} to find arrays so

Article.where(tags: '{xbox}')

这不会带来任何错误,但它并没有任何发现任何

This doesn't bring any errors but it doesn't find anything either

#=> #<Article id: 4, name: "Playstation 3", price: 150.0, description: "testing description and tags", created_at: "2015-07-06 21:29:24", updated_at: "2015-07-06 21:29:24", user_id: 1, category_id: 2, puerto_rico_city_id: 1, tags: ["", "playstation 3", "playstation", "xbox", "test"]>

如果我

Article.where(tags: '{"", "playstation 3", "playstation", "xbox", "test"}')

这会找到它,但我只是希望它找到它时,你只输入数组中的一个。

This will find it, but I just want it to find it when you type just one of the arrays.

感谢

推荐答案

找到了答案,这样的:

Article.where('标签@&GT;','{}的Xbox')

这会发现每个文章的Xbox的标签

编辑:

下面是一个不同的语法,做同样的:

here's a different syntax which does the same:

Article.where(['?= ANY(标签)','的Xbox'])

这篇关于查找论文中,数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:40