目前,我通过定义关系模型,然后通过关系模型设置has_many关系来实现多对多关系。像这样的东西:
class WorldCup < ActiveRecord::Base
has_many :country_taggings#, :as => :entity
has_many :countries, :through => :country_taggings
end
class Country < ActiveRecord::Base
has_many :country_taggings
end
class CountryTaggings < ActiveRecord::Base
belongs_to :country
belongs_to :world_cup
# belongs_to :entity, :polymorphic => true
end
当然,这很容易转换成has_and_belongs_to_many,如果它不是我在那里注释的东西。因此,从父母到关系模型的关系是多态的。实际定义中间关系模型的详细程度正在扼杀我。难道没有办法重新发现has_and_belongs_to_many并以某种方式找到了一种方法来实现多态性吗?
答案 0 :(得分:0)
试试这个 -
http://ruby-on-rails-dipak-panchal.blogspot.in/2012/10/has-many-through-relationship.html
class WorldCup < ActiveRecord::Base
has_many :country_taggings
has_many :countries, :through => :country_taggings
end
class Country < ActiveRecord::Base
has_many :country_taggings
has_many :worldcups, :through => :country_taggings
end
class CountryTaggings < ActiveRecord::Base
belongs_to :country
belongs_to :world_cup
end
也看到了这个
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
答案 1 :(得分:0)
class WorldCup < ActiveRecord::Base
has_many :country_taggings
has_many :countries, :through => :country_taggings
end
class Country < ActiveRecord::Base
has_many :country_taggings
end
class CountryTaggings < ActiveRecord::Base
belongs_to :entity, :polymorphic => true
belongs_to :country
belongs_to :world_cup
end