has_many :通过多个 has_one 关系?

发布于 2024-10-17 03:50:25 字数 408 浏览 5 评论 0原文

我正在用 Rails 为我们的教堂编写一个指导计划(我对 Rails 还很陌生)。

我需要对此进行建模。

contact
has_one :father, :class_name => "Contact"
has_one :mother, :class_name => "Contact"
has_many :children, :class_name => "Contact"
has_many :siblings, :through <Mother and Father>, :source => :children

所以基本上一个对象“兄弟姐妹”需要映射来自父亲和母亲的所有孩子,而不是包括物体本身..

这可能吗?

谢谢

丹尼尔

I'm writing a mentorship program for our church in rails (im still farily new to rails)..

And i need to model this..

contact
has_one :father, :class_name => "Contact"
has_one :mother, :class_name => "Contact"
has_many :children, :class_name => "Contact"
has_many :siblings, :through <Mother and Father>, :source => :children

So basically an objects "siblings" needs to map all the children from both the father and mother not including the object itself..

Is this possible?

Thanks

Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-10-24 03:50:25

有趣的是,看似简单的问题却有复杂的答案。在这种情况下,实现反射性父/子关系相当简单,但添加父/母和兄弟姐妹关系会产生一些麻烦。

首先,我们创建表来保存父子关系。关系有两个外键,都指向联系人:

create_table :contacts do |t|
  t.string :name
end

create_table :relationships do |t|
  t.integer :contact_id
  t.integer :relation_id
  t.string :relation_type
end

在关系模型中,我们将父亲和母亲指向联系人:

class Relationship < ActiveRecord::Base
  belongs_to :contact
  belongs_to :father, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'father'}}
  belongs_to :mother, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'mother'}}
end

并在联系人中定义逆关联:

class Contact < ActiveRecord::Base
  has_many :relationships, :dependent => :destroy
  has_one :father, :through => :relationships
  has_one :mother, :through => :relationships
end

现在可以创建关系:

@bart = Contact.create(:name=>"Bart")
@homer = Contact.create(:name=>"Homer")
@bart.relationships.build(:relation_type=>"father",:father=>@homer)
@bart.save!
@bart.father.should == @homer

这不是很好,这是我们真正想要的是在一次调用中建立关系:

class Contact < ActiveRecord::Base
  def build_father(father)
    relationships.build(:father=>father,:relation_type=>'father')
  end
end

所以我们可以这样做:

@bart.build_father(@homer)
@bart.save!

要查找联系人的子级,请向联系人添加范围和(为了方便起见)实例方法:

scope :children, lambda { |contact| joins(:relationships).\
  where(:relationships => { :relation_type => ['father','mother']}) }

def children
  self.class.children(self)
end

Contact.children(@homer) # => [Contact name: "Bart")]
@homer.children # => [Contact name: "Bart")]

兄弟姐妹是棘手的部分。我们可以利用 Contact.children 方法并操纵结果:

def siblings
  ((self.father ? self.father.children : []) +
   (self.mother ? self.mother.children : [])
   ).uniq - [self]
end

这不是最佳的,因为father.children 和 mother.children 会重叠(因此需要 uniq),并且可以更有效地完成通过制定必要的 SQL(作为练习:)),但请记住 self.father.childrenself.mother.children 不会重叠在同父异母兄弟姐妹(同父异母)的情况下,联系人可能没有父亲或母亲。

以下是完整的型号和一些规格:

# app/models/contact.rb
class Contact < ActiveRecord::Base
  has_many :relationships, :dependent => :destroy
  has_one :father, :through => :relationships
  has_one :mother, :through => :relationships

  scope :children, lambda { |contact| joins(:relationships).\
    where(:relationships => { :relation_type => ['father','mother']}) }

  def build_father(father)
    # TODO figure out how to get ActiveRecord to create this method for us
    # TODO failing that, figure out how to build father without passing in relation_type
    relationships.build(:father=>father,:relation_type=>'father')
  end

  def build_mother(mother)
    relationships.build(:mother=>mother,:relation_type=>'mother')
  end

  def children
    self.class.children(self)
  end

  def siblings
    ((self.father ? self.father.children : []) +
     (self.mother ? self.mother.children : [])
     ).uniq - [self]
  end
end

# app/models/relationship.rb
class Relationship < ActiveRecord::Base
  belongs_to :contact
  belongs_to :father, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'father'}}
  belongs_to :mother, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'mother'}}
end

# spec/models/contact.rb
require 'spec_helper'

describe Contact do
  before(:each) do
    @bart = Contact.create(:name=>"Bart")
    @homer = Contact.create(:name=>"Homer")
    @marge = Contact.create(:name=>"Marge")
    @lisa = Contact.create(:name=>"Lisa")
  end

  it "has a father" do
    @bart.relationships.build(:relation_type=>"father",:father=>@homer)
    @bart.save!
    @bart.father.should == @homer
    @bart.mother.should be_nil
  end

  it "can build_father" do
    @bart.build_father(@homer)
    @bart.save!
    @bart.father.should == @homer
  end

  it "has a mother" do
    @bart.relationships.build(:relation_type=>"mother",:father=>@marge)
    @bart.save!
    @bart.mother.should == @marge
    @bart.father.should be_nil
  end

  it "can build_mother" do
    @bart.build_mother(@marge)
    @bart.save!
    @bart.mother.should == @marge
  end

  it "has children" do
    @bart.build_father(@homer)
    @bart.build_mother(@marge)
    @bart.save!
    Contact.children(@homer).should include(@bart)
    Contact.children(@marge).should include(@bart)
    @homer.children.should include(@bart)
    @marge.children.should include(@bart)
  end

  it "has siblings" do
    @bart.build_father(@homer)
    @bart.build_mother(@marge)
    @bart.save!
    @lisa.build_father(@homer)
    @lisa.build_mother(@marge)
    @lisa.save!
    @bart.siblings.should == [@lisa]
    @lisa.siblings.should == [@bart]
    @bart.siblings.should_not include(@bart)
    @lisa.siblings.should_not include(@lisa)
  end

  it "doesn't choke on nil father/mother" do
    @bart.siblings.should be_empty
  end
end

It's funny how questions that appear simple can have complex answers. In this case, implementing the reflexive parent/child relationship is fairly simple, but adding the father/mother and siblings relationships creates a few twists.

To start, we create tables to hold the parent-child relationships. Relationship has two foreign keys, both pointing at Contact:

create_table :contacts do |t|
  t.string :name
end

create_table :relationships do |t|
  t.integer :contact_id
  t.integer :relation_id
  t.string :relation_type
end

In the Relationship model we point the father and mother back to Contact:

class Relationship < ActiveRecord::Base
  belongs_to :contact
  belongs_to :father, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'father'}}
  belongs_to :mother, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'mother'}}
end

and define the inverse associations in Contact:

class Contact < ActiveRecord::Base
  has_many :relationships, :dependent => :destroy
  has_one :father, :through => :relationships
  has_one :mother, :through => :relationships
end

Now a relationship can be created:

@bart = Contact.create(:name=>"Bart")
@homer = Contact.create(:name=>"Homer")
@bart.relationships.build(:relation_type=>"father",:father=>@homer)
@bart.save!
@bart.father.should == @homer

This is not so great, what we really want is to build the relationship in a single call:

class Contact < ActiveRecord::Base
  def build_father(father)
    relationships.build(:father=>father,:relation_type=>'father')
  end
end

so we can do:

@bart.build_father(@homer)
@bart.save!

To find the children of a Contact, add a scope to Contact and (for convenience) an instance method:

scope :children, lambda { |contact| joins(:relationships).\
  where(:relationships => { :relation_type => ['father','mother']}) }

def children
  self.class.children(self)
end

Contact.children(@homer) # => [Contact name: "Bart")]
@homer.children # => [Contact name: "Bart")]

Siblings are the tricky part. We can leverage the Contact.children method and manipulate the results:

def siblings
  ((self.father ? self.father.children : []) +
   (self.mother ? self.mother.children : [])
   ).uniq - [self]
end

This is non-optimal, since father.children and mother.children will overlap (thus the need for uniq), and could be done more efficiently by working out the necessary SQL (left as an exercise :)), but keeping in mind that self.father.children and self.mother.children won't overlap in the case of half-siblings (same father, different mother), and a Contact might not have a father or a mother.

Here are the complete models and some specs:

# app/models/contact.rb
class Contact < ActiveRecord::Base
  has_many :relationships, :dependent => :destroy
  has_one :father, :through => :relationships
  has_one :mother, :through => :relationships

  scope :children, lambda { |contact| joins(:relationships).\
    where(:relationships => { :relation_type => ['father','mother']}) }

  def build_father(father)
    # TODO figure out how to get ActiveRecord to create this method for us
    # TODO failing that, figure out how to build father without passing in relation_type
    relationships.build(:father=>father,:relation_type=>'father')
  end

  def build_mother(mother)
    relationships.build(:mother=>mother,:relation_type=>'mother')
  end

  def children
    self.class.children(self)
  end

  def siblings
    ((self.father ? self.father.children : []) +
     (self.mother ? self.mother.children : [])
     ).uniq - [self]
  end
end

# app/models/relationship.rb
class Relationship < ActiveRecord::Base
  belongs_to :contact
  belongs_to :father, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'father'}}
  belongs_to :mother, :foreign_key => :relation_id, :class_name => "Contact",
  :conditions => { :relationships => { :relation_type => 'mother'}}
end

# spec/models/contact.rb
require 'spec_helper'

describe Contact do
  before(:each) do
    @bart = Contact.create(:name=>"Bart")
    @homer = Contact.create(:name=>"Homer")
    @marge = Contact.create(:name=>"Marge")
    @lisa = Contact.create(:name=>"Lisa")
  end

  it "has a father" do
    @bart.relationships.build(:relation_type=>"father",:father=>@homer)
    @bart.save!
    @bart.father.should == @homer
    @bart.mother.should be_nil
  end

  it "can build_father" do
    @bart.build_father(@homer)
    @bart.save!
    @bart.father.should == @homer
  end

  it "has a mother" do
    @bart.relationships.build(:relation_type=>"mother",:father=>@marge)
    @bart.save!
    @bart.mother.should == @marge
    @bart.father.should be_nil
  end

  it "can build_mother" do
    @bart.build_mother(@marge)
    @bart.save!
    @bart.mother.should == @marge
  end

  it "has children" do
    @bart.build_father(@homer)
    @bart.build_mother(@marge)
    @bart.save!
    Contact.children(@homer).should include(@bart)
    Contact.children(@marge).should include(@bart)
    @homer.children.should include(@bart)
    @marge.children.should include(@bart)
  end

  it "has siblings" do
    @bart.build_father(@homer)
    @bart.build_mother(@marge)
    @bart.save!
    @lisa.build_father(@homer)
    @lisa.build_mother(@marge)
    @lisa.save!
    @bart.siblings.should == [@lisa]
    @lisa.siblings.should == [@bart]
    @bart.siblings.should_not include(@bart)
    @lisa.siblings.should_not include(@lisa)
  end

  it "doesn't choke on nil father/mother" do
    @bart.siblings.should be_empty
  end
end
冰葑 2024-10-24 03:50:25

我完全同意泽泰蒂克的观点。这个问题看起来比答案简单得多,我们对此无能为力。不过我会加上 20c。
表:

    create_table :contacts do |t|
      t.string :name
      t.string :gender
    end
    create_table :relations, :id => false do |t|
      t.integer :parent_id
      t.integer :child_id
    end

表关系没有对应的模型。

class Contact < ActiveRecord::Base
  has_and_belongs_to_many :parents,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'child_id',
    :association_foreign_key => 'parent_id'

  has_and_belongs_to_many :children,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'parent_id',
    :association_foreign_key => 'child_id'

  def siblings
    result = self.parents.reduce [] {|children, p| children.concat  p.children}
    result.uniq.reject {|c| c == self}
  end

  def father
    parents.where(:gender => 'm').first
  end

  def mother
    parents.where(:gender => 'f').first
  end
end  

现在我们有定期的 Rails 协会。所以我们可以

alice.parents << bob
alice.save

bob.chidren << cindy
bob.save

alice.parents.create(Contact.create(:name => 'Teresa', :gender => 'f')

做所有类似的事情。

I totally agree with zetetic. The question looks far more simpler then the answer and there is little we could do about it. I'll add my 20c though.
Tables:

    create_table :contacts do |t|
      t.string :name
      t.string :gender
    end
    create_table :relations, :id => false do |t|
      t.integer :parent_id
      t.integer :child_id
    end

Table relations does not have corresponding model.

class Contact < ActiveRecord::Base
  has_and_belongs_to_many :parents,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'child_id',
    :association_foreign_key => 'parent_id'

  has_and_belongs_to_many :children,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'parent_id',
    :association_foreign_key => 'child_id'

  def siblings
    result = self.parents.reduce [] {|children, p| children.concat  p.children}
    result.uniq.reject {|c| c == self}
  end

  def father
    parents.where(:gender => 'm').first
  end

  def mother
    parents.where(:gender => 'f').first
  end
end  

Now we have regular Rails assosiations. So we can

alice.parents << bob
alice.save

bob.chidren << cindy
bob.save

alice.parents.create(Contact.create(:name => 'Teresa', :gender => 'f')

and all stuff like that.

找个人就嫁了吧 2024-10-24 03:50:25
  has_and_belongs_to_many :parents,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'child_id',
    :association_foreign_key => 'parent_id',
    :delete_sql = 'DELETE FROM relations WHERE child_id = #{id}'

  has_and_belongs_to_many :children,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'parent_id',
    :association_foreign_key => 'child_id',
    :delete_sql = 'DELETE FROM relations WHERE parent_id = #{id}'

我使用了这个示例,但必须添加 :delete_sql 来清理关系记录。起初我在字符串周围使用双引号,但发现这会导致错误。切换到单引号有效。

  has_and_belongs_to_many :parents,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'child_id',
    :association_foreign_key => 'parent_id',
    :delete_sql = 'DELETE FROM relations WHERE child_id = #{id}'

  has_and_belongs_to_many :children,
    :class_name => 'Contact',
    :join_table => 'relations',
    :foreign_key => 'parent_id',
    :association_foreign_key => 'child_id',
    :delete_sql = 'DELETE FROM relations WHERE parent_id = #{id}'

I used this example but had to add the :delete_sql to clean up the relations records. At first I used double quotes around the string but found that caused errors. Switching to single quotes worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文