[英]How to update a column of type array with postgresql and ruby on rails
[英]Query for array type field on Ruby on Rails and PostgreSQL
我不知道怎么写一个查询。 问题:
我有两个表, authorities
和notices
每个都有这个列:
t.string "tags", default:[], array:true
我需要检查来自authorities
数组的至少一个元素是否在notices
数组中,并将其保存在类变量中。 到目前为止,我已经在我的控制器中尝试了这行代码:
@noticias = Notice.where('tags @> ARRAY[?]',current_authority.tags)
我在视图中尝试过类似的东西:
<% @noticias.each do |notice| %>
<% notice.nombre %>
<% end %>
编辑
谢谢你的答案,但我的问题是
ERROR: operator does not exist: character varying[] @> text[]
解决方案是:
@noticias = Notice.where('tags && ARRAY[?]::varchar[]',current_authority.tags)
如此处所述, 如果数组包含值
您可能希望使用overlap
运算符而不是contain
。 contain
运算符A @> B
表示A包含A @> B
所有元素。如果我理解您要执行的操作,则需要检查Notice
的标记数组是否包含当前权限的任何标记。
你可以这样做:
@noticias = Notice.where('tags && ARRAY[?]', current_authority.tags)
这是Postgres中所有数组函数的链接: http : //postgresql.org/docs/current/static/functions-array.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.