的has_many:通过问题 [英] has_many :through questions

查看:197
本文介绍了的has_many:通过问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是previously使用has_and_belongs_to_many,并已转化为HAS_MANY:通过。下面是它的外观为游戏,可以有很多的用户播放列表。有了这个,我可以做game.users和user.games ....:

I was previously using has_and_belongs_to_many, and have converted to has_many :through. Here's how it looks for a list of games that can have many users playing. With this, I can do game.users and user.games....:

class Game < ActiveRecord::Base
 has_many :game_users, :dependent => :destroy
 has_many :users, :through => :game_users, :uniq => true
end

class User < ActiveRecord::Base
 has_many :game_users, :dependent => :destroy
 has_many :games, :through => :game_users, :uniq => true
end

class GameUser < ActiveRecord::Base
  belongs_to :game
  belongs_to :user
end

和用于连接表我的数据库迁移:

And my database migration for the join table:

create_table :game_users, :id => false do |t|
      t.column :game_id, :integer
      t.column :user_id, :integer
      t.column :player_index, :integer
    end

我不太确定我得到这一切,请帮我查一下我的情况:

I'm not quite sure I get all this, please help me check my facts:

  1. 是因=>:摧毁是否正确?我想'game_users如果加盟无论是游戏还是用户被破坏的表项将被删除 - ?但我不想,如果游戏被删除的用户将被删除,反之亦然......

  1. Is the dependent => :destroy correct? I want the 'game_users' join table entry to be deleted if either the game or the user is destroyed - but I don't want users to be deleted if games are deleted and vice versa..... ?

该Uniq字段应该说,比赛只包含独特的用户,而用户只包含独特的游戏。这是否正确?

The uniq field is supposed to say that games contain only unique users, and the users only contain unique games. Is that correct?

数据库迁移像以前那样有:ID =>假的。是不是仍然是正确的事是什么?我试着在控制台摧毁一个游戏,并得到有关失踪ID的抱怨......所以我猜不会和试图理解为什么。

The database migration as before has :id => false. Is that still the right thing to do? I tried in the console destroying a game, and got complaints about the missing id... so I'm guessing not and trying to understand why.

我觉得轨活动记录的关联非常混乱。我猜他们是不应该!

I find rails active record associations very confusing. I guess they aren't supposed to be!

推荐答案

1:是的,这是正确的。

1: Yes, that's correct

2:从上uniq中的文档

2: From the documentation on uniq:

如果属实,副本将被省略   从集合。有用的   会同:通过

If true, duplicates will be omitted from the collection. Useful in conjunction with :through.

所以,是的,如果你的目标是进不去游戏的用户收集相同的游戏用户的游戏收集,也没有相同的用户,这是正确的。这一切都说明这里

So, yes, if you aim is not get the same game in User's games-collection nor the same user in Game's users-collection, that's correct. It's all explained here.

它不会,但是,要创建从prevent重复GameUsers。对于这一点,你需要使用<一个href="http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates%5Funiqueness%5Fof">validates_在GameUser模型的唯一性_of :

It will not, however, prevent duplicate GameUsers from being created. For that, you'd need to use validates_ uniqueness _of in the GameUser-model:

class GameUser < ActiveRecord::Base
  validates_uniqueness_of :game_id, :scope => :user_id
end

3:不,你不希望使用:ID =>假了。通过从has_and_belongs_to_many切换到HAS_MANY:通过,你已经晋升的许多一对多连接表,以一个完整的模型 - GameUser - 这都需要有自己的ID

3: No, you don't want to use :id => false any more. By switching from has_and_belongs_to_many to has_many :through, you have promoted your many-to-many join-table to a full model - GameUser - which requires its own id.

虽然是旧的,仍是一个好文章理解的has_many:通过。

While it is old, this is still a good article for understanding has_many :through.

这篇关于的has_many:通过问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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