2017-08-31 104 views
0

您可以使用tagged_with找到标记的对象。rails:如何基于acts_as_taggable_on关联标签进行查询?

class User < ActiveRecord::Base 
    acts_as_taggable_on :tags, :skills 
    scope :by_join_date, order("created_at DESC") 
end 

User.tagged_with("awesome").by_join_date 

但你如何找到标记对象的关联?

class UserAccount < ActiveRecord::Base 
    belongs_to :user 
end 

UserAccount.joins(:user)...??? 

回答

1

UserAccount.joins(:user).merge(User.tagged_with("awesome"))

或者你可以使用反向查询:

User.tagged_with("awesome").includes(:user_account).

查询选择取决于你的目标。

相关问题