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 qu开发者_开发问答alify it by what tag(s) are set on a record - is there an easy way to do this?
e.g. 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'])
).
精彩评论