AtaurRehman Asad
AtaurRehman Asad

Reputation: 173

belongs_to relationship not working

I have following active record class

class Car < ActiveRecord::Base
  belongs_to :owner
end

in the code when I try this

Car.first.owner

it gives me error "undefined method owner"

Can any one plz let me now if I'm missing any thing

Upvotes: 3

Views: 121

Answers (1)

Raindal
Raindal

Reputation: 3237

You need to write the relation on the Owner side : has_one :car or has_many :cars depending on your needs.

class Car < ActiveRecord::Base
    belongs_to :owner
end

class Owner < ActiveRecord::Base
    has_one :car
end

Upvotes: 7

Related Questions