Lets suppose we have a football player, this player can have one or two teams ONLY, I mean he may be in a club team and/or a national team or nothing of them. what is the best way to do this in rails relation?
Share
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.
I would create
has_onerelationships for each team type. Perhaps you call oneclub_teamand the othernational_team. Don’t make these relationships required. This would accomplish your goal of allowing 0, 1, or 2 relationships between players and teams.You can accomplish this by renaming the relationship in your model. For instance, if you had a model named
FootballTeamthen you could have two relationships like this:This would also provide you with methods like
player.club_teamandplayer.national_teamifplayerwere an instance of your player model.