0

I wonder how I create an internal messagerie like twitter or Facebook between two users with Ruby on rails.

What is the table with Main and foreigners keys do I use?

Have I to do Messagerie/id/User_id/ But there are two Users.

Which belongs_to and which has has_many?

Do you know what I mean. Thanks for your precious help.

Hussein.
  • 179
  • 2
  • 11
  • Possible duplicate of http://stackoverflow.com/questions/5120703/creating-a-many-to-many-relationship-in-rails-3 – Pavel Oganesyan Mar 18 '16 at 07:17
  • @PavelOganesyan Thank you my question is how to create an intern Messenger? What can I do nest to have an interaction between 2 users? – Hussein. Mar 18 '16 at 12:34
  • Ok, let's consider another approach. If you don't need chats with multiple users you can make message with 2 fields - `senderID` and `receiverID`. When current user opens chat with another one you can select all messages where these 2 users are sender or receiver. – Pavel Oganesyan Mar 18 '16 at 12:55
  • @PavelOganesyan That looks interesting. If I understand the table will be: Senders/id/user_id/message_id & Receivers/id/sender_id/message_id but if they answer, the sender will be receiver and vice-verca how could you controll this? – Hussein. Mar 18 '16 at 13:04
  • No, table Messages will have columns Id, Text, Sender, Receiver, Sent_at. You can select messages for 2 users as `Messages.where('sender IN (?) and receiver IN (?)', %w(user1Id, user2Id), %w(user1Id,user2Id))` – Pavel Oganesyan Mar 18 '16 at 13:16
  • @PavelOganesyan Do I place this following code `Messages.where('sender IN (?) and receiver IN (?)', %w(user1Id, user2Id), %w(user1Id,user2Id))` in my controller? – Hussein. Mar 18 '16 at 14:12
  • Yes, in the method that shows chats. But it depends on design of your application. Such code can also be placed in model. I guess, you need a good tutorial about basics of RoR applications design. Maybe, this one with chat example - http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails It uses quite same scheme but adds Conversation objects. – Pavel Oganesyan Mar 20 '16 at 15:23
  • @PavelOganesyanThank you the tutorial looks like full! – Hussein. Mar 21 '16 at 19:08

0 Answers0