Relationship_to doesn't work

I have an active record class

class Car < ActiveRecord::Base
  belongs_to :owner
end

      

in code when i try to do this

Car.first.owner

      

it gives me the error "undefined method owner"

Can anyone plz allow me now if I miss something?

+3


source to share


1 answer


You need to write a relationship from the owner's 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

      

+7


source







All Articles