Let say I have a Group
of User
s and User
has a enum, which described it's mood.
假设我有一组用户,用户有一个枚举,描述了它的情绪。
class Group < AR::Base
has_many :users
class User < AR::Base
belongs_to :group
enum mood: %i(good bad ugly)
How can I find all groups, where at least one User
has the good mood?
我怎样才能找到所有团体,其中至少有一个用户心情愉快?
Which index should I add for optimizing this query?
我应该添加哪个索引来优化此查询?
1 个解决方案
#1
1
You can use joins
method.
您可以使用连接方法。
Group.joins(:users).where("users.mood = ?", User.moods[:good])
You can add this into your migration
您可以将其添加到迁移中
add_index :users, :group_id
#1
1
You can use joins
method.
您可以使用连接方法。
Group.joins(:users).where("users.mood = ?", User.moods[:good])
You can add this into your migration
您可以将其添加到迁移中
add_index :users, :group_id