简体   繁体   English

在Rails3 Active Record查询上使用acts_as_taggable_on?

[英]Using acts_as_taggable_on on Rails3 Active Record queries?

If I want to select a group of records from a database using a SQL query and further qualify it by what tag(s) are set on a record - is there an easy way to do this? 如果我想使用SQL查询从数据库中选择一组记录,然后通过在记录上设置哪些标签进一步对其进行限定-是否有一种简便的方法?

eg something conceptually like Select * from Corps where date_added > '2010-01-01' and tag like "legal" 例如,在概念上类似“ Select * from Corps where date_added > '2010-01-01' and tag like "legal"

Obviously this isn't correct since the tag data isn't kept in the table, but you get the idea. 显然,这是不正确的,因为标签数据没有保留在表中,但是您知道了。

Rails3 has a new syntax for Active Record queries. Rails3具有用于Active Record查询的新语法。 Can I select a group of records using the AREL AR syntax, then apply the tag find operation to that? 我可以使用AREL AR语法选择一组记录,然后对其应用标签查找操作吗?

Yes, with the tagged_with() method. 是的,使用tagged_with()方法。

Example using your sample SQL query: 使用示例SQL查询的示例:

@corps = Corp.where('date_added > 2010-01-01').tagged_with('legal')

You can also put an array of tags into tagged_with ( tagged_with(['legal', 'foo']) ). 您还可以将标签数组放入agged_with( tagged_with(['legal', 'foo']) )中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM