Ruby on rails 你有很多而且你属于多态的吗?
可能重复:Ruby on rails 你有很多而且你属于多态的吗?,ruby-on-rails,activerecord,polymorphic-associations,Ruby On Rails,Activerecord,Polymorphic Associations,可能重复: 目前,我正在通过定义关系模型实现多对多关系,然后通过关系模型设置has_many关系。大概是这样的: class WorldCup < ActiveRecord::Base has_many :country_taggings#, :as => :entity has_many :countries, :through => :country_taggings end class Country < ActiveRecord::Base has
目前,我正在通过定义关系模型实现多对多关系,然后通过关系模型设置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
class世界杯:实体
有许多:国家,:至=>:国家标签
结束
类国家/地区true
结束
当然,如果不是因为我在那里评论的东西,这将很容易翻译成has_和wo belies_to_many。因此,从父对象到关系模型的关系是多态的。实际上定义中间关系模型的冗长性让我很难受。难道没有一种方法可以让你重新拥有和属于很多人,并找到一种方法来改变它吗?试试这个-
class世界杯:国家标签
结束
类国家/地区:国家杯
结束
类CountryTaggings
也看到这个
世界杯:国家标签
结束
类国家/地区ActiveRecord::Base
有很多:国家标签
结束
类CountryTaggingstrue
属于:国家
属于:世界杯
结束
对不起,这正是我上面描述的。。。这不是我问题的答案
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
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