我不知道如何编写查询。问题:查询Ruby on Rails和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)
不存储阵列中的DB。曾经 –
@AndreyDeineko:为什么不呢? SQL支持数组,PostgreSQL支持数组,而Rails4 PostgreSQL接口也如此。这不像他在使用“序列化”的憎恶。 –
@ muistooshort我从来没有试图驳斥它。我只有一些经验,这些经验告诉我,总有(应该是'几乎总是'写成,但在我的情况下总是)更好的方式,即协会。 –