This works for top-level categories, but if I use the slug of a sub-category, it gives me
> category = Category.find_by_slug(category_slug)
=> nil
How do you achieve the same for sub-categories?
Edit:
I think I figured it out (thanks to this hint). This seems to work, but there might be a more elegant way (I don’t know how to define and hand over an array)
# levels: :watching :watching_first_post
level = :watching
category_slug = "local-news"
parent_category_slug = "news" # <== added
category = Category.find_by_slug(category_slug, parent_category_slug)
group_name = "trust_level_0" # all users
group = group = Group.find_by_name(group_name)
group.users.each do |user|
watched_categories = CategoryUser.lookup(user, level).pluck(:category_id)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[level],
category.id) unless watched_categories.include?(category.id)
end