I am making a joint party matching app.
In other words, it is a matching app for multiple people to multiple people.
I want to make a conditional branch so that my favorite users can chat with each other, but the conditional branch is too complicated to understand.

I will give you an overview of the app.
The models currently in use are the User model, the UserInfo model, and the RoomUser model.
The UserInfo model describes the information (area, number of people) posted by User.
You can post only one per user.
Other users can add the posted information to their favorites.
I want to be able to chat when each other likes each other's posts.

The problem this time is the conditional branch that "when each other likes each other's posts".
I don't understand here, and I'm worried about half a day. .. (;;)

Corresponding source code
class RoomsController

↑ Chat room controller.
I want to make a conditional branch in the new action here.

favorites table
Column Type Options
user_id string null: false
user_info_id string null: false
  • belongs_to: user
  • belongs_to: user_info
favorites database
user_id user_info_id
1 1 2
2 1 Four
3 2 Four
Four Four Five
Five 2 1
6 1 6

This is a simple representation of the state of Sequel Pro.
The leftmost column is the id.
The user_id column is the favorite user.
The user_info_id column is a favorite post.
Here, think that the number of user_info_id and the id of the user who posted it are the same.

What I tried
class RoomsController

I wrote rooms_controller as above. ..

I thought it was wrong, so I'm not doing it.
Is there anyone who can do this conditional branching?
We look forward to hearing from you (; ∀ ;)

  • Answer # 1

    I can't answer ...
    I think there is a smarter way, but I think you can get the expected value below as well.

    Each other liked each other's posts

    # Get a list of your favorite users
    fav_users = Favorite.where (user_id: current_user.id)
     # Get users who are your favorite and who are your favorite
    Favorite.each do | fav_users |
      fav_both_users = Favorite.where (user_id: fav_users) .where (user_info_id: current_user.id)