0

I am looking for a design approach for the following. I have three models [regions, groups, items] which are used to generate a tree menu (ancestry gem)

class Region < ActiveRecord::Base
 has_many :groups
 has_many :items
end


class Group < ActiveRecord::Base
 belongs_to :region
 has_many :items
 has_ancestry
end

class Item < ActiveRecord::Base
 belongs_to :region
 belongs_to :group
end

As you can see I have a region assigned to both group and item for the purpose of building a menutree: Region(s) => Group(s) => Item(s).

The Item's region is used extensively throughout the application however the group's region is used only when rendering the menu.

I don't like that the group's region may be different to any of its item's regions or indeed any of its descendants. I have been assured that inheriting the region for the item from the group is not appropriate as an item may not have a group, nor can the group inherit its region from the unique regions of the item (since if there were items of different regions contained by the same group the same group would appear twice under different regions)

Can anyone suggest another approach to this?

4

1 に答える 1

0

私は持っていてGroupedItem、それはそのままUngroupedItem継承しItemます:

class Item < ActiveRecord::Base
end

class GroupedItem < Item
  belongs_to :group
end

class UngroupedItem < Item
  belongs_to :region
end

テーブルにtype列が必要です。ActiveRecord を使用した単一テーブルの継承に関する詳細情報。Items

于 2012-04-17T08:05:18.440 に答える