用于连接表连接两个记录在同一个表的ActiveRecord的关系

用于连接表连接两个记录在同一个表的ActiveRecord的关系

本文介绍了用于连接表连接两个记录在同一个表的ActiveRecord的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人物模型和链路模型。领汇模式重新presents从角色到另一个角色的链接。链接有文字描述属性。从字母A到人物B链接有别于相反的链接从B到A的字符具有零个或一个链接到另一个角色。一个角色可以有不同的链接到不同的字符。一个角色可以通过各种不同的角色联系到。

I have a Character model and a Link model. The Link model represents a link from a character to another character. A link has a textual 'description' attribute. A link from character A to character B is distinct from the opposite link from B to A. A character has zero or one link to another character. A character may have various links to different characters. A character may be linked to by various different characters.

我用用Active Record的关系,部分执行字符和链接模型之间的关系:

I used used Active Record relationships to partly implement relationships between Character and Link models:

class Character
 has_many :links # the links from the character to other characters

class Link
 belongs_to :character # the character from which starts the link to another character

这给了我有用的方法,比如character.links(从这个字符开始的所有环节的阵列)或link.character(字符从开始的链接)

which give me useful methods like character.links (array of all links starting from this character) or link.character (character from which starts the link)

链接模式也有一个 to_character_id 包含字符谁去联系的ID。因此,从字符A到人物B的链接是一个具有以下属性的一个实例:

The link model has also a to_character_id which contains the id of the character to who goes the link. Thus a link from character A to character B is an instance with the following attributes:

  • character_id =角色ID A
  • to_character_id =字母B的ID
  • 说明 =一些文本
  • character_id = id of character A
  • to_character_id = id of character B
  • description = some text

我已经写了各种额外的方法,如 character.links_to (返回指向该字符的所有环节的数组)或 link.to_character (返回字符,它指向的链接),或 character.characters_who_link_to (返回的有一个链接,这个角色的其他字符数组) 。我已经写了也是一个回调,以确保当一个字符被删除,哪去了这个角色的所有链接被删除(相同的恢复)。

I've written various extra methods like character.links_to (returning an array of all links which points to the character) or link.to_character (returning the character to which points the link), or character.characters_who_link_to (returning an array of the other characters having a link to this character). I've written also a callback to make sure that when a character is deleted, all links which go to this character are deleted (same for restoration).

可以使用额外的AR关系的声明这会为我提供这样的额外方法,这样我就不用自己写这些方法和回调?

Is possible to use additional AR relationships declarations which would provide me with this kind of extra methods, so that I do not have to write myself those methods and callbacks?

敏捷Web开发使用Rails presents在第一个解决方案使用型号为联接表,但对于一个连接表连接两个不同的表。就我而言我的连接表链接加入一个表的记录,人物。

Agile Web Development with Rails presents a solution in the section "Using Models as Join Tables" but for a join table joining two different tables.In my case my join table Links join records of a single table, Characters.

推荐答案

has_​​and_belongs_to_many 是不是真的使用了;我会使用的has_many:通过而不是

has_and_belongs_to_many is not really in use anymore; I'd use has_many :through instead.

class Character < ActiveRecord::Base
   has_many :links, :dependent => destroy
   has_many :characters, :through => :links

   has_many :source_links, :class_name => "Link",
     :foreign_key => "to_character_id", :dependent => :destroy
   has_many :source_characters, :class_name => "Character",
     :through => :destination_links
 end

 class Link < ActiveRecord::Base
   belongs_to :character
   belongs_to :source_character, :class_name => "Character",
    :foreign_key => "to_character_id"
 end

注意:依赖=&GT; :摧毁选项 - 当角色被删除,这些将删除的链接。命名是正确的 - 但从人物的角度, source_links 的链接的的性格。所以,现在你可以这样做:

Note the :dependent => :destroy options - these will delete the links when the character is deleted. The naming is right - from the character's point of view, source_links are links to that character. So now you can do:

@character.characters # characters I link to
@character.links # links I have to other characters
@character.source_characters # characters that link to me
@character.source_links # links other characters have to me

这篇关于用于连接表连接两个记录在同一个表的ActiveRecord的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 15:04