Skip to content

Instantly share code, notes, and snippets.

@hxx
Created July 8, 2014 13:55
Show Gist options
  • Save hxx/dc95849d3d3079ed9a29 to your computer and use it in GitHub Desktop.
Save hxx/dc95849d3d3079ed9a29 to your computer and use it in GitHub Desktop.
has_many :through
class Play < ActiveRecord::Base
# 先告诉model我们在体育协会有很多笔资料
has_many :sports_associations
# 这些资料是要拿来判断这个球员有参与多少球队
has_many :teams, :through => :sports_associations
end
class SportsAssociations < ActiveRecord::Base
# 体育协会要对球员和球队负责,所以体育球队belongs_to球员和球队
belongs_to :team
belongs_to :player
end
class Team < ActiveRecord::Base
# 先告诉model我们在体育协会有很多笔资料
has_many :sports_associations
# 这些资料是要拿来判断这个球队有多少球员
has_many :players, :through => :sports_associations
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment