I’m creating a model called Chats. And I want to assign users to a discussion. They are either a part of the Chats or they aren’t…
So I create one model Chats.
What’s the standard Rails naming convention for the other table?
ChatUsers?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While
has_and_belongs_to_manyis an ok option here, I recommend going withhas_many :throughinstead.In essence you will have an explicit join model, which you can call something like
ChatSession.Now you will need a table called
chat_sessionswith columns :user_id, and :chat_id in it. This is your join table.Advantage
You get a model which is fully under your control, and isn’t just a dumb join table managed by rails. So for example, if you want to track number of messages particular user left in particular chat, it could be a column in
chat_sessionstable. Presence of:throughrendershabtmunneeded in most cases. There is no complexity overhead either.