[英]rails 4 - using has_and_belongs_to_many association
I have two types of data: Team
and Game
. 我有两种类型的数据:
Team
和Game
。 A team can belong to more than one game (eg. a college can play basketball, football, and soccer), and each game naturally has many teams that play in the league. 一个团队可以属于一个以上的比赛(例如,大学可以打篮球,橄榄球和足球),并且每场比赛自然都有很多球队参加联赛。
class Game < ActiveRecord::Base
has_and_belongs_to_many :teams
end
class Team < ActiveRecord::Base
has_and_belongs_to_many :games
end
There's a table in the db with two columns to hold these relationships: 数据库中有一个表,其中有两列来保存这些关系:
create_table "games_teams", id: false, force: true do |t|
t.integer "game_id"
t.integer "team_id"
end
So far so good. 到现在为止还挺好。
I need to build a form to add new Teams
but I can't get the Game
to show up correctly: 我需要建立一个表格来添加新的
Teams
但是我无法使Game
正确显示:
<%= form_for @team, :html => {:class => 'form-horizontal form', :role => 'form'} do |f| %>
<%= f.collection_select(:game_id, Game.all, :id, :name) %>
This throws an error undefined method game_id for #<Team:0x007fa1af68f2d8>
这会
undefined method game_id for #<Team:0x007fa1af68f2d8>
引发错误的undefined method game_id for #<Team:0x007fa1af68f2d8>
How do I properly insert game_id
when working with a Team
object, and team_id
when adding a Game
? 在使用
Team
对象时如何正确插入game_id
,在添加Game
时如何正确插入team_id
? I realize these ought to handle a "multi-select" scenario, but at the moment I can't get any of it to work. 我意识到这些应该可以解决“多选”情况,但是目前我还无法解决任何问题。
Thanks! 谢谢!
The answer you're looking for is to use game_ids[]
instead: 您正在寻找的答案是使用
game_ids[]
代替:
#View
<%= f.collection_select(:game_ids, Game.all, :id, :name) %>
#Controller
def create
@team = Team.new(Team_params)
@team.save
end
private
def team_params
params.require(:team).permit(game_ids: [])
end
This should set the collection values for has_and_belongs_to_many
data. 这应该设置
has_and_belongs_to_many
数据的收集值。 I'll have to test this when I get into the office - it definitely works, but whether the syntax is right is what I've got to find 我到办公室时必须对此进行测试-肯定可以,但是语法是否正确才是我所要找到的
The first parameter on collection_select
is an attribute from the object that is been described. collection_select
上的第一个参数是来自所描述对象的属性。 So, this line 所以这条线
f.collection_select(:game_id, Game.all, :id, :name) f.collection_select(:game_id,Game.all,:id,:name)
Try to call game_id
on the object team
, which fails. 尝试调用对象
team
上的game_id
,但失败。
I'm not sure if there is a direct method supported by rails to help you construct this associations. 我不确定rails是否支持直接方法来帮助您构建此关联。 You can always construct a solution with
select_tag
and options_for_select
(with mutiple: true
since it is a has_and_belongs_to_many
, send an array of game_ids to your action and deal with it manualy. 您始终可以使用
select_tag
和options_for_select
构造一个解决方案(使用mutiple: true
因为它是has_and_belongs_to_many
,将一个game_ids数组发送到您的操作并手动处理。
You might also take a look on this rails cast 您还可以在此栏杆上看一看
Try using association instead of collection_select 尝试使用关联而不是collection_select
<%= form_for @team, :html => {:class => 'form-horizontal form', :role => 'form'} do |f| %>
<%= f.association :game, collection: Game.all.map{|p| [p.id, p.name]}, :label => 'Drive', :prompt => "Select Game" %>
I believe you want to use has_many through instead of has_and_belongs_to_many. 我相信您想使用has_many而不是has_and_belongs_to_many。 like:
喜欢:
class Game < ActiveRecord::Base
has_many :game_teams
has_many :teams, :through => :game_teams
end
class Team < ActiveRecord::Base
has_many :game_teams
has_many :games, :through => :game_teams
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.