I have an app that is intended to sort all user articles by identical user fields. An example of this is if a user wanted to see all other user articles from users with a similar occupation. Each user has an occupation (string) field in the database. I've been able to filter all users with identical occupations by using this query:
similar_users = User.where(occupation: current_user.occupation)
I have been unsuccessful at filtering their articles by similar_users. Articles have an author_id which is equal to the user.id.
I've tried chains such as Articles.similar_users, Articles.similar_users.where(author_id: user.id), or Article.all.where(author_id: similar_users.id), but they don't work and rightly so. How would I go about chaining these classes? Is there more effective way, such as using named scopes with lambdas?
In Article.rb
belongs_to(
:user,
class_name: "User",
foreign_key: :author_id,
primary_key: :id
)
In User.rb
has_many(
:articles,
class_name: "Article",
dependent: :destroy
)