What is the "rails way" to access a parent object's attributes?

activerecord, model, ruby, ruby-on-rails

Solution

You could use delegate.

class Patient
    belongs_to :doctor
    delegate :office, :to => :doctor
end

You could have multiple attributes in one delegate method.

class Patient
    belongs_to :doctor
    delegate :office, :address, :to => :doctor
end

Problem

Let's say I have a model `Doctor`, and a model `Patient`. A `Patient belongs_to a Doctor`. A `Doctor` has an attribute `office`. I would want to, given a `Patient p`, be able to say `p.office` and access the `office` of `p`'s Doctor. I could always write a method ``` class Patient belongs_to :doctor def office self.doctor.office end ``` But is there a more automatic way to expose all of the `Doctor`'s attribute methods to the `Patient`? Perhaps using `method_missing` to have some kind of catch-all method?

Original source