関係が必ずしも階層的であるとは限らない (つまり、親と子ではなく、友人と同僚) ある個人と別の個人との関係をモデル化したいと考えています。 、 設立日)。最後に、act_as_tree 関係を使用して、これらの関係をナビゲート/図解できるようにしたいと思います。
移行:
class CreateProfiles < ActiveRecord::Migration def self.up
create_table :profiles do |table|
table.column :firstName, :string
table.column :lastName, :string
table.column :telephone, :string
table.column :emailAddress, :string
table.column :location, :string
table.timestamps
end end def self.down
drop_table :profiles end end
class Relationship < ActiveRecord::Migration def self.up
create_table :relationships, :id => false do |table|
table.column my_id :integer
table.column your_id :integer
table.column relationshipType :string
table.column dateEstablished :date
table.column notes :text
table.timestamps end end def self.down
drop_table :relationships end end
モデル:
class Person < ActiveRecord::Base
has_many :relationships, :foreign_key => "my_id"
has_many :persons :through => :relationships
end
class Relationship < ActiveRecord::Base
belongs_to :persons
acts_as_tree
end
質問:
- これらのテーブル間の関係を正しく定義するにはどうすればよいですか?
- my_id/your_id の組み合わせは一意であるため、リレーションシップ テーブルで :id を削除することは理にかなっていますか?
- RoR の規則を利用するための「my_id」および「your_id」フィールドのより適切な名前はありますか?
- 列の 1 つが名前 'parent_id' でない場合、act_as_tree 関係で問題が発生しますか?