0

我在使用自引用表时遇到了麻烦。

我有一个可以容纳行星、恒星和卫星的球体模型。我想告诉一件事“环绕”另一件事

我看了一下rails guide,但我可以让它工作

我的模型:

class Orb < ActiveRecord::Base
  belongs_to :orb_type
  has_and_belongs_to_many :books

  belongs_to :orbit, :class_name => "Orb"
  has_many :orbs, :class_name => "Orb", :foreign_key => "orb_id"

  attr_accessible :descr, :nome, :orb_type_id, :book_ids, :orb_id

  validates :nome, uniqueness: true, presence: true
end

我想我使用了不好的关系名称(可能是错误的方式)

1.9.3-p448 :002 > earth = Orb.find(1)
  Orb Load (0.2ms)  SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1  [["id", 1]]
 => #<Orb id: 1, nome: "Terra", descr: "123123", orb_type_id: 1, created_at: "2013-09-25 14:53:35", updated_at: "2013-09-25 14:57:40", orb_id: nil> 
1.9.3-p448 :003 > moon = Orb.find(2)
  Orb Load (0.2ms)  SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1  [["id", 2]]
 => #<Orb id: 2, nome: "Lua", descr: "asd", orb_type_id: 2, created_at: "2013-09-25 14:53:46", updated_at: "2013-09-25 14:55:31", orb_id: nil> 
1.9.3-p448 :004 > sun = Orb.find(3)
  Orb Load (0.2ms)  SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1  [["id", 3]]
 => #<Orb id: 3, nome: "Sol", descr: "asd", orb_type_id: 3, created_at: "2013-09-25 14:53:55", updated_at: "2013-09-25 14:53:55", orb_id: nil> 
1.9.3-p448 :006 > moon.orbit=earth
 => #<Orb id: 1, nome: "Terra", descr: "123123", orb_type_id: 1, created_at: "2013-09-25 14:53:35", updated_at: "2013-09-25 14:57:40", orb_id: nil> 
1.9.3-p448 :007 > earth.orbit=sun
 => #<Orb id: 3, nome: "Sol", descr: "asd", orb_type_id: 3, created_at: "2013-09-25 14:53:55", updated_at: "2013-09-25 14:53:55", orb_id: nil> 
1.9.3-p448 :008 > earth
 => #<Orb id: 1, nome: "Terra", descr: "123123", orb_type_id: 1, created_at: "2013-09-25 14:53:35", updated_at: "2013-09-25 14:57:40", orb_id: nil> 
1.9.3-p448 :009 > sun
 => #<Orb id: 3, nome: "Sol", descr: "asd", orb_type_id: 3, created_at: "2013-09-25 14:53:55", updated_at: "2013-09-25 14:53:55", orb_id: nil> 
1.9.3-p448 :010 > moon
 => #<Orb id: 2, nome: "Lua", descr: "asd", orb_type_id: 2, created_at: "2013-09-25 14:53:46", updated_at: "2013-09-25 14:55:31", orb_id: nil> 

最后什么都没有关联,FK仍然没有。

collun orb_id 是后来在模型上添加的。我设置了迁移并将其添加到模型中。我不认为这可能与我的问题有关...


编辑:

现在一切都变得奇怪了。我将模型更改为:

class Orb < ActiveRecord::Base
  belongs_to :orb_type
  has_and_belongs_to_many :books

  belongs_to :orbit, :class_name => "Orb"
  has_many :orbits, :class_name => "Orb", :foreign_key => "orb_id"

  attr_accessible :descr, :nome, :orb_type_id, :book_ids, :orb_id

  validates :nome, uniqueness: true, presence: true
end

在rails控制台(rails c)中,我尝试:

1.9.3-p448 :008 > earth = Orb.find(1)
  Orb Load (0.2ms)  SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1  [["id", 1]]
 => #<Orb id: 1, nome: "Terra", descr: "", orb_type_id: 1, created_at: "2013-09-25 17:51:26", updated_at: "2013-09-25 18:16:58", orb_id: 3> 
1.9.3-p448 :009 > earth.orbit
 => nil 
1.9.3-p448 :010 > earth.orbits
  Orb Load (0.3ms)  SELECT "orbs".* FROM "orbs" WHERE "orbs"."orb_id" = 1
 => [#<Orb id: 2, nome: "Lua", descr: "", orb_type_id: 2, created_at: "2013-09-25 17:51:40", updated_at: "2013-09-25 18:17:31", orb_id: 1>] 
1.9.3-p448 :011 > 

有没有搞错?轨道可以返回我想要的,但我尝试使用它:

1.9.3-p448 :004 > earth.orbits.nome
NoMethodError:   Orb Load (0.4ms)  SELECT "orbs".* FROM "orbs" WHERE "orbs"."orb_id" = 1
undefined method `nome' for #<ActiveRecord::Relation:0x00000003d593c0>
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.12/lib/active_record/relation/delegation.rb:45:in `method_missing'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.12/lib/active_record/associations/collection_proxy.rb:100:in `method_missing'
        from (irb):4
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
4

2 回答 2

0

从技术上讲,在同一个模型中拥有行星、恒星和卫星并不是很好。他们都有不同的行为,应该各自保证自己的模型。例如,恒星永远不会围绕行星运行,而行星永远不会围绕月球运行(根据定义)。通过按照您的方式构建数据库,您正在为损坏的数据设置自己,如果您允许,这种情况总会发生。

如果您必须将它们全部包含在Orb模型中,我会推荐 2 个模型:1 个用于Orbs(恒星、行星、卫星),1 个用于Orbits. 该类Orbit本质上是一个像这样的表:

Orbit model:

| id | orbited_id | orbiter_id |
--------------------------------
|  0 | planet_id  | moon_id    |
|  1 | star_id    | planet_id  |
|  2 | planet_id  | moon_id    |
| ...| etc        | etc        |

然后你可以在 orb.rb 中设置关联:

has_and_belongs_to_many :orbits,
                        class_name: 'Orb',
                        join_table: :orbits,
                        foreign_key: :orbited_id,
                        association_foreign_key: :orbiter_id,
                        uniq: true

这将允许您执行以下操作

>> sun = Orb.find(1)
>> sun.orbits
=> [ <Orb mercury>, <Orb venus>, ..., <Orb pluto> ]
于 2013-09-25T16:41:59.220 回答
0

我想通了。我应该使用关系名称(orbit_id)来制作表中的rom,而不是使用表名(orb_id)

修复一切开始正常工作:)

has_many :orbters, class_name: "Orb", foreign_key: "orbit_id"
belongs_to :orbit, class_name: "Orb"
于 2013-09-26T22:08:53.483 回答