从rails中的关联模型访问父模型的ID

时间:2012-01-04 19:52:17

标签: ruby-on-rails model associations

好的,我在rails中有以下型号:

  • 'Shards'
  • '现实',它们属于碎片或属于任何东西
  • 属于现实的'空间'
  • 属于空格的'属性'

我可以访问shard.reality.space.first.properties,但是如果以这种方式访问​​过,我希望能够从属性模型中的自定义方法访问“shard”id。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

class Property < ActiveRecord::Base
  belongs_to :space

  def custom_method
    space.reality.shard.id if space.reality.shard
  end
end