Rails的型号用户将播放器的用户插件板 [英] Rails Model user adding player to user pinboard
问题描述
我要创建一个体育应用程序,允许用户创建掲示板,尽可能多的,因为他们喜欢。然后,用户可以添加说出自己最喜欢的球员到指定的插件板。因此,例如, 用户:杰克,创建一个新的插件板被称为我最喜欢的维护者,然后,他可以去找他最喜欢的捍卫者,说凯文·石榴石,并添加/引脚石榴石他的我最喜爱的捍卫者插件板。
我有一个用户模式,掲示板型和球员模型。我尝试了以下的关联,但没有工作,我想要的。
用户
的has_many:掲示板
的has_many:引脚,通过:掲示板,来源::播放器
插件板
belongs_to的:用户
belongs_to的:播放器
我知道一个球员不能有许多掲示板和插接板将举行众多玩家。我怎样才能得到我想要的行为;用户创建插件板---用户将播放器插件板。
用户
的has_many:pin_boards
的has_many:引脚,通过:pin_boards,来源:玩家
插接板
belongs_to的:用户
的has_many:pin_board_player_assignments
的has_many:玩家通过:pin_board_player_assignments
PinBoardPlayerAssignment
belongs_to的:pin_board
belongs_to的:播放器
播放器
的has_many:pin_board_player_assignments
的has_many:pin_boards,通过:pin_board_player_assignments
所以,基本上,你需要添加另一种模式叫做 PinBoardPlayerAssignment
。从那里,我会强烈建议使用此连接模型来创建和管理任务,例如:
PinBoardPlayerAssignment
高清self.create_between(a_pin_board,a_player)
分配= a_pin_board.pin_board_player_assignments.build
assignment.player = a_player
assignment.save!
结束
这样,您就可以使用实例方法对插件板
或在播放
来冷冰冰地创建转接板和球员之间的分配。基本上,我们是知道最关心pin_boards和球员的对象是人去管它们之间的关系。
插件板
高清add_player(a_player)
PinBoardPlayerAssignment.create_between(个体经营,a_player)
结束
播放器
高清add_to_pin_board(a_pin_board)
PinBoardPlayerAssignment.create_between(a_pin_board,个体经营)
结束
和伟大的事情是,Rails的3.2+允许嵌套:通过关系,让你的原来的要求 - 有 @ user.pins
应编制的经历,结果pin_board然后通过pin_board_player_assignment表自动
I'm creating a sports application which allows users to create pinboards, as many as they like. The users can then add say their favorite player to a specified pinboard. So for example User: Jack, creates a new pinboard called "My favorite defenders", He can then go and find his favorite defenders, say Kevin Garnet, and add/pin garnet to his "My favorite defenders" Pinboard.
I have a Users model, a Pinboards Model and a Players model. I tried the following association but didn't work as I wanted.
User
has_many :pinboards
has_many :pins, through: pinboards, source: :player
Pinboard
belongs_to :user
belongs_to :player
I know a player can't have many pinboards and a pinboard will hold many players. How can I get the behavior i want; User creates pinboard---user adds player to pinboard.
User
has_many :pin_boards
has_many :pins, through: :pin_boards, source: :players
PinBoard
belongs_to :user
has_many :pin_board_player_assignments
has_many :players, through: :pin_board_player_assignments
PinBoardPlayerAssignment
belongs_to :pin_board
belongs_to :player
Player
has_many :pin_board_player_assignments
has_many :pin_boards, through: :pin_board_player_assignments
So, basically, you'll need to add another model called PinBoardPlayerAssignment
. From there, I'd highly recommend using this join model to create and manage the assignments, like so:
PinBoardPlayerAssignment
def self.create_between(a_pin_board, a_player)
assignment = a_pin_board.pin_board_player_assignments.build
assignment.player = a_player
assignment.save!
end
This way, you can use instance methods on PinBoard
or on Player
to DRYly create the assignment between the PinBoard and the Player. Basically, let the object that knows the most about pin_boards and players be the one to manage the relationships between them.
PinBoard
def add_player(a_player)
PinBoardPlayerAssignment.create_between(self, a_player)
end
Player
def add_to_pin_board(a_pin_board)
PinBoardPlayerAssignment.create_between(a_pin_board, self)
end
And the great thing is that Rails 3.2+ allows nested :through relationships so your original request -- to have @user.pins
should compile the result of going through the pin_board and then through the pin_board_player_assignment table automatically.
这篇关于Rails的型号用户将播放器的用户插件板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!