开发者

Question about Association and Models on Rails

I have been working on a project recently where a Player can create a Team and be the Team Owner, but a player as well can be part of the Team by a separate table, named Squad.

class Player
has_many :teams
has_many :squads
en开发者_Python百科d

class Squad
belongs_to :player
belongs_to :team
end

class Team
belongs_to :owner, :class_name => "Player"
has_many :squads
has_many :players, :through => "squads"
end

I don't know if this is all I need to make what I want, but I just can't figure out. How can I make the player ask to be invited to the team via Squad, and the Owner of the Team answers yes or no to that player? If yes, he joins table Squad and is part of the team. If no, his request is destroyed.


You need to make a boolean field status in your Squad model, that defaults to false. If you need something more complex, you could use this gem.

So player invokes a squad#create action, it creates a squad model. Team#show shows the owner of the team join requests from players and use squad#accept or squad#reject (or #update with status argument) and then you change the status to true, or destroy this squad record.

And basically that's it

Updated

This is how a basic social network friending system works.

With state_machine you could define another state rejected, so the same user doesn't spam the team owner with requests after being rejected. By checking the updated_at field, you could implement a timeout, after which the same person could repeat his request.


It looks like you need to learn about has_and_belongs_to_many relationships. That's what you should be using to associate teams with players, squads with players, and squads with teams.

As for the invitation, you should probably create a model called Invitation that is associated with players/squads/teams in the appropriate way. Notification about invitations can be handled in the controllers/views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜