有关协会和模型on Rails的问题 [英] Question about Association and Models on Rails

查看:154
本文介绍了有关协会和模型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
end

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.

推荐答案

您需要在你的球队模型中的布尔字段状态,即默认为false。如果你需要更复杂的东西,你可以使用这种宝石

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.

所以玩家调用一个小队#创建操作,它会创建一个小队模式。
团队#秀展示了球队的老板从加盟的球员和球队的使用要求#接受或小队#拒收(或状态参数#UPDATE),然后将状态更改为true,或破坏这支球队的纪录。

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.

基本上就是这样

更新

这是一个基本的社交网络邀请好友系统是如何工作的。

This is how a basic social network friending system works.

通过state_machine你可以定义拒绝另一种状态,所以相同的用户不会被拒绝的请求后,垃圾邮件的球队老板。通过检查的updated_at字段,则可以实现超时,在此之后,同一个人可重复他的请求。

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.

这篇关于有关协会和模型on Rails的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆