开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜