简体   繁体   English

belongs_to关系不起作用

[英]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 如果我遗漏任何东西,任何人都可以让我现在

You need to write the relation on the Owner side : has_one :car or has_many :cars depending on your needs. 您需要在所有者方面编写关系: has_one :carhas_many :cars具体取决于您的需求。

class Car < ActiveRecord::Base
    belongs_to :owner
end

class Owner < ActiveRecord::Base
    has_one :car
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM