belongs_to的HAS_ONE结构 [英] belongs_to has_one structure
问题描述
我有具有以下特征的应用程序
有俱乐部 每个俱乐部都有团队 每支球队都有球员
我有一个用户表。用户表基本上包含用户名和密码为俱乐部经理,团队经理和玩家登录到系统中。
我应该如何结构化模型和表格?
我打算创建表的俱乐部,球队和球员。但我不知道展会上构建他们和用户表之间的关系。
我可以在每个模型的创建 USER_ID
,但这种关系是俱乐部belongs_to的用户
这没有按'牛逼似乎是正确的。而且我会最终有一个用户模式,具有以下
HAS_ONE:俱乐部
HAS_ONE:团队
HAS_ONE:球员
这是不正确的。的用户将其中只有一个在任何给定的时间。
有没有更好的方式来组织呢?
在Rails中, HAS_ONE
真的最多有一个。这是完全有效的有三个 HAS_ONE
装饰在用户
。如果你想确保他们只有precisely之一,你可以添加一个验证,例如:
类用户的LT;的ActiveRecord :: Base的
HAS_ONE:俱乐部
HAS_ONE:团队
HAS_ONE:球员
验证:has_only_one
私人
高清has_only_one
如果[俱乐部,球队,球员] .compact.length!= 1
errors.add_to_base(必须有$ P $的俱乐部,球队或球员pcisely 1)
结束
结束
结束
既然你已经更改数据库中的用户表的能力,我想我会放在 club_id
, TEAM_ID
, player_id
在用户
,并具备以下条件:
类俱乐部LT;的ActiveRecord :: Base的
HAS_ONE:用户
的has_many:团队
的has_many:玩家:通过=> :团队
结束
一流的团队及LT;的ActiveRecord :: Base的
HAS_ONE:用户
belongs_to的:俱乐部
的has_many:玩家
结束
一流的球员和LT;的ActiveRecord :: Base的
HAS_ONE:用户
belongs_to的:团队
HAS_ONE:俱乐部:通过=> :球队
结束
类用户的LT;的ActiveRecord :: Base的
belongs_to的:俱乐部
belongs_to的:团队
belongs_to的:播放器
验证:belongs_to_only_one
高清belongs_to_only_one
如果[俱乐部,球队,球员] .compact.length!= 1
errors.add_to_base(必须属于$ P $的俱乐部,球队或球员pcisely 1)
结束
结束
结束
我甚至想要重命名用户
为管理
,或者 HAS_ONE :经理:将class_name => 用户
在俱乐部
,团队
和播放器
的车型,但你的电话。
I have an application which has the following characteristics
There are Clubs Each Club has Teams Each Team has Players
I have a users table. The user table basically contains the username and password for the club manager, team manager and the player to login to the system.
How should I structure the models and the tables?
I plan to create tables for Club, Team and Players. But I am not sure show to structure the relationship between them and the users table.
I could create user_id
in each of the model, but the relationship would be Club belongs_to User
which doesn't seems right. Moreover I would end up with a User model that has the following
has_one :club
has_one :team
has_one :player
Which is not right. A user will have only one of them at any given time.
Is there a better way to structure this?
Under Rails, has_one
is really "has at most one". It's perfectly valid to have all three has_one
decorators in User
. If you want to ensure they only have precisely one, you could add a validation, for instance:
class User < ActiveRecord::Base
has_one :club
has_one :team
has_one :player
validate :has_only_one
private
def has_only_one
if [club, team, player].compact.length != 1
errors.add_to_base("Must have precisely one of club, team or player")
end
end
end
Since you have the ability to change the users table in the database, I think I would put club_id
, team_id
, player_id
in users
, and have the following:
class Club < ActiveRecord::Base
has_one :user
has_many :teams
has_many :players, :through => :teams
end
class Team < ActiveRecord::Base
has_one :user
belongs_to :club
has_many :players
end
class Player < ActiveRecord::Base
has_one :user
belongs_to :team
has_one :club, :through => :team
end
class User < ActiveRecord::Base
belongs_to :club
belongs_to :team
belongs_to :player
validate :belongs_to_only_one
def belongs_to_only_one
if [club, team, player].compact.length != 1
errors.add_to_base("Must belong to precisely one of club, team or player")
end
end
end
I'd even be tempted to rename User
as Manager
, or have has_one :manager, :class_name => "User"
in the Club
, Team
and Player
models, but your call.
这篇关于belongs_to的HAS_ONE结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!