简体   繁体   中英

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?

eg something conceptually like 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. Can I select a group of records using the AREL AR syntax, then apply the tag find operation to that?

Yes, with the tagged_with() method.

Example using your sample SQL query:

@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']) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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