Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在ruby on rails的seeds.rb中填充联接(多对多)表?_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何在ruby on rails的seeds.rb中填充联接(多对多)表?

Ruby on rails 如何在ruby on rails的seeds.rb中填充联接(多对多)表?,ruby-on-rails,Ruby On Rails,我目前有一个team.rb和user.rb的模型,这是一种多对多关系。我已经创建了联接表teams\u用户,但我不确定如何在myseeds.rb中填充此表 例如,我有: user = User.create({ first_name: 'Kamil', last_name: 'Bo', email: 'bo@gmail.com'}) team = Team.create([{ name: 'Spot Forwards', num_of_games: 10, day_of_play: 4}])

我目前有一个team.rb和user.rb的模型,这是一种多对多关系。我已经创建了联接表teams\u用户,但我不确定如何在myseeds.rb中填充此表

例如,我有:

user = User.create({ first_name: 'Kamil', last_name: 'Bo', email: 'bo@gmail.com'})
team = Team.create([{ name: 'Spot Forwards', num_of_games: 10, day_of_play: 4}])
但是下面的方法不起作用

TeamsUsers.create({ team_id: team.id, user_id: user.id })
我得到一个信息:

uninitialized constant TeamsUsers

这不是优化的,而是

user.team_ids = user.team_ids < team.id
user.save

还可以开始使用has_many:through。然后您将拥有一个TeamUser模型。如果联接表需要更多的属性,这将是一个救命稻草这不是优化的,而是

user.team_ids = user.team_ids < team.id
user.save

还可以开始使用has_many:through。然后您将拥有一个TeamUser模型。如果联接表需要更多的属性,那么它将是一个救命稻草

选择一个要从中工作的边,然后按照@drhenner的建议,使用
\u id
属性创建关联。例如,使用
用户
模型,首先创建团队,然后创建用户,并在运行时将其分配给团队:

teams = Team.create([
  { name: 'Team 1' },
  { name: 'Team 2' },
  { name: 'Team 3' },

  # etc.
])

User.create([
  { name: 'User 1', team_ids: [teams[0].id, teams[2].id] },
  { name: 'User 2', team_ids: [teams[1].id, teams[2].id] },
  { name: 'User 3', team_ids: [teams[0].id, teams[1].id] },

  # etc.
])

根据以上评论:

您可以在
has\u many:to
关系上配置多个关系。由您决定要实现哪些功能。这些都是可能的:

class Team < ApplicationRecord
  has_many :memberships
  has_many :users, through: :memberships
end

class Membership < ApplicationRecord
  belongs_to :team
  belongs_to :user
end

class User < ApplicationRecord
  has_many :memberships
  has_many :teams, through: :memberships
end

这将为您提供
team.users
user.teams

选择一方进行工作,然后按照@drhenner的建议,使用
\u id
属性创建关联。例如,使用
用户
模型,首先创建团队,然后创建用户,并在运行时将其分配给团队:

teams = Team.create([
  { name: 'Team 1' },
  { name: 'Team 2' },
  { name: 'Team 3' },

  # etc.
])

User.create([
  { name: 'User 1', team_ids: [teams[0].id, teams[2].id] },
  { name: 'User 2', team_ids: [teams[1].id, teams[2].id] },
  { name: 'User 3', team_ids: [teams[0].id, teams[1].id] },

  # etc.
])

根据以上评论:

您可以在
has\u many:to
关系上配置多个关系。由您决定要实现哪些功能。这些都是可能的:

class Team < ApplicationRecord
  has_many :memberships
  has_many :users, through: :memberships
end

class Membership < ApplicationRecord
  belongs_to :team
  belongs_to :user
end

class User < ApplicationRecord
  has_many :memberships
  has_many :teams, through: :memberships
end

这将为您提供使用Rails的
team.users
user.teams

假设您有表
foos
bar
在多对多关系中使用表
foos\u bars
,您可以像这样为它们的关联播种:

bar1 = Bar.find(1)
bar2 = Bar.find(2)

foo1 = Foo.find(1) # for example
foo1.bars << bar1
foo1.bars << bar2
foo1.save
bar1=Bar.find(1)
bar2=Bar.find(2)
foo1=Foo.find(1)#例如

foo1.bar使用Rails,假设您有表
foos
bar
在多对多关系中使用表
foos\u bar
,您可以像下面这样为它们的关联播种:

bar1 = Bar.find(1)
bar2 = Bar.find(2)

foo1 = Foo.find(1) # for example
foo1.bars << bar1
foo1.bars << bar2
foo1.save
bar1=Bar.find(1)
bar2=Bar.find(2)
foo1=Foo.find(1)#例如

foo1.bars我会在团队和用户身上放一个has_-many:通过吗?这取决于你。请看我下面的帖子。我会在团队和用户身上都写一个has_-many:通吗?这取决于你。见下面我的帖子。