コレクションでデメテルの法則を使用して満たすためにモデルでデータを繰り返すことは適切ですか?

StackOverflow https://stackoverflow.com/questions/6404354

  •  29-10-2019
  •  | 
  •  

質問

これは不自然な例です。私は、人が友人を持っている国の人口をリストしたいと言っています。以下に2つのセットアップがあります。モデルでデータを繰り返すのが最善でしょうか?

私はそれを言った デメテルの法則 従うことが重要です。例は、犬に歩くように言うことです。足を歩くように命じるのは愚かです。

私の豊かな経験の中で(NOOB)、モデルがデータを繰り返すときにクエリをするのがはるかに簡単であることがわかりました。 People.where(:country => friend.country), 、vsコレクションが鎖の関連性がある(これまで不可能だった): People.where(:city => { :county => { :region => { :country => friend.city.county.region.country }}}) (ここでこのnoobが概念を理解するのに本当に役立つでしょう。正しい不自然なLODセットアップと構文を想像できれば、デメテルの法則とは何の関係もない例を使用しなかったことを本当に願っています)私はLODを適用しようとしました経由 delegate そして、私はまだチェーンしていると言われました(私は私です)、私が考えることができる唯一の解決策は、協会からアクセスできるデータを繰り返すことです。

しかし、私はデータを繰り返すのが嫌いです!これは、Twitterを再作成するDHHのRails Tutuorialをフォローしているため、関係と繰り返しデータを作成することがどれほど素晴らしいかを示しました。

繰り返しのデータは、関連性を鎖で抑えるために適切である必要がありますか?

モデル、繰り返しデータ

class Country < ActiveRecord::Base    
  has_many :regions    
  has_many :counties    
  has_many :cities    
  has_many :people
end

class Region < ActiveRecord::Base
  has_one :country
  has_many :counties
  has_many :cities    
  has_many :people
end

class County < ActiveRecord::Base
  has_one :country
  has_one :region
  has_many :cities    
  has_many :people
end

class City < ActiveRecord::Base
  has_one :country
  has_one :region
  has_one :county    
  has_many :people
end

class Person < ActiveRecord::Base
  has_one :country
  has_one :region
  has_one :county    
  has_one :city
  has_many :relationships
  has_many :friends, :through => :relationships
end

鎖のある関連性を持つモデルとモデル

class Country < ActiveRecord::Base    
  has_many :regions   
end

class Region < ActiveRecord::Base
  belongs_to :country
  has_many :counties
end

class County < ActiveRecord::Base
  belongs_to :region
  has_many :cities
end

class City < ActiveRecord::Base
  belongs_to :county
end

class Person < ActiveRecord::Base
  belongs_to :city
end

正しい解決策はありません

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top