自我指导有很多麻烦

时间:2013-09-25 15:18:42

标签: ruby-on-rails-3 has-many self-join

我在自我参考表上遇到了麻烦。

我得到了一个可以容纳行星,恒星和卫星的球体模型。我想告诉一件事“轨道”另一个

我看看导轨指南,但我可以开始工作

我的模特:

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 console(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>'

2 个答案:

答案 0 :(得分:0)

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

如果你必须在Orb模型中拥有它们,我建议使用2个模型:Orbs(星星,行星,卫星)为1,Orbits为1。 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> ]

答案 1 :(得分:0)

我明白了。我应该使用关系名称(orbit_id)在表中创建rom,而不是使用表名(orb_id)

修复一切都开始很好地运作:)

has_many :orbters, class_name: "Orb", foreign_key: "orbit_id"
belongs_to :orbit, class_name: "Orb"