I am using the ancestry gem to organize my post categories tree. I have 8 parent categories, and they all have many child categories. To retrieve an array of child categories, I use:
parent_category.children
I defined these relations in my category
model:
has_ancestry
has_many :posts
has_many :users, through: :posts
The last relation allows me to find the users who post in each category.
What I am trying to do: on first login, users have to choose their categories of interest. On the next screen, they are suggested users who post in the categories they chose.
To find the suggested users, I wrote a method:
def recommendations
@users = []
(current_user.get_voted Category).each do |category|
category.children.each do |cat|
@users << cat.users
end
end
@users = @users.flatten.uniq
end
This works, but it seems overly complicated. Is there any way to do this in a "best practice" way ?